---
title: "How to Replace Character in Vi Editor: Best Examples"
description: "In the Vi text editor, you can replace characters, words, or even patterns using a variety of commands. Here are some of the top commands for replacing characters in Vi. https://srinimf.com/2023/01/14..."
url: https://srinimf.com/2023/10/10/how-to-replace-character-in-vi-editor-best-examples/
date: 2023-10-10
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2023/10/pexels-photo-7319132.jpeg?fit=1880%2C1246&ssl=1
categories: ["Linux"]
tags: ["Vi Editor"]
type: post
lang: en
---

# How to Replace Character in Vi Editor: Best Examples

In the Vi text editor, you can replace characters, words, or even patterns using a variety of commands. Here are some of the top commands for replacing characters in Vi.

Related[Linux egrep Vs. fgrep: Top Differences](https://srinimf.com/2023/01/14/linux-egrep-vs-fgrep-top-differences/)

![Vi editor replace](https://i0.wp.com/srinimf.com/wp-content/uploads/2023/10/pexels-photo-6045219.jpeg?resize=861%2C1300&ssl=1)*Photo by Skylar Kang on Pexels.com*

## Vi Editor: Here are commands to replace Character(s)

1. **Replace Single Character at Cursor Position:**
   - `r` followed by the character you want to replace the current character with. For example, to replace the character under the cursor with ‘X’, press `rx`.
2. **Replace Multiple Characters:**
   - `R` (capital ‘R’) puts Vi into replace mode. You can then type over existing characters, and they will be replaced one by one as you type.
3. **Replace All Occurrences of a Character in a Line:**
   - `:s/old/new/g` – This command replaces all occurrences of ‘old’ with ‘new’ in the current line. Replace ‘old’ and ‘new’ with your desired characters.
4. **Replace All Occurrences of a Character in the Entire File:**
   - `:%s/old/new/g` – This command replaces all occurrences of ‘old’ with ‘new’ throughout the entire file.
5. **Replace a Specific Word:**
   - `:s/old/new/g` – Replace ‘old’ with ‘new’ in the current line. To replace only the first occurrence in each line, omit the ‘g’ flag.
6. **Replace a Specific Pattern:**
   - You can use regular expressions with the `:s` command to replace patterns. For example, to replace all occurrences of ‘apple’ with ‘banana’, use `:%s/apple/banana/g`.
7. **Replace Within a Range of Lines:**
   - To replace within a specific range of lines, specify the range before the `:s` command. For example, to replace ‘old’ with ‘new’ in lines 5 to 10, use `:5,10s/old/new/g`.
8. **Confirm Replacements:**
   - To confirm each replacement, add the ‘c’ flag to the `:s` command. For example, `:%s/old/new/gc` will prompt you to confirm each replacement.
9. **Replace in Visual Mode:**
   - Enter Visual mode by pressing `v`, select the text you want to replace, and then press `:` to enter command-line mode. You can then use `s/old/new/g` to replace the selected text.
10. **Global Replace (Without Confirmation):**
    - To replace all occurrences of a character globally without confirmation, you can use the `:g` command. For example, `:%s/old/new/g` will replace ‘old’ with ‘new’ on all lines, and `:g/old/s//new/g` will do the same but without confirmation.

Remember that Vi operates in different modes (normal, insert, and command line), so you may [need to](https://srinimf.com/2018/04/08/popular-influencers/) switch between these modes appropriately when using these commands. To save your changes after making replacements, press `**Esc**` to enter normal mode and then use the **`:wq` **command to save and quit or `**:w**` to save the changes without quitting.

Related

- [Vi Editor Command: How to Replace Last Characters](https://srinimf.com/2023/10/06/vi-editor-command-how-to-replace-last-characters/)DateOctober 6, 2023
- [COBOL Picture clause Characters](https://srinimf.com/2015/12/16/complete-list-of-cobol-picture-clause-charecters/)DateDecember 16, 2015
- [QMF: EXPORT Report to Dataset](https://srinimf.com/2010/09/04/qmf-export-report-to-dataset/)DateSeptember 4, 2010

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2023%2F10%2F10%2Fhow-to-replace-character-in-vi-editor-best-examples%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2023%2F10%2Fpexels-photo-7319132.jpeg%3Ffit%3D1200%252C795%26ssl%3D1&description=How%20to%20Replace%20Character%20in%20Vi%20Editor%3A%20Best%20Examples)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2023/10/10/how-to-replace-character-in-vi-editor-best-examples/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2023/10/10/how-to-replace-character-in-vi-editor-best-examples/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2023/10/10/how-to-replace-character-in-vi-editor-best-examples/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2023/10/10/how-to-replace-character-in-vi-editor-best-examples/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Replace%20Character%20in%20Vi%20Editor%3A%20Best%20Examples&body=https%3A%2F%2Fsrinimf.com%2F2023%2F10%2F10%2Fhow-to-replace-character-in-vi-editor-best-examples%2F&share=email)
-

### Like this:

Like Loading…
