This guide explains the r and r commands in Vim, the popular text editor for Linux and Unix systems. In Vim, r replaces a single character under the cursor, while r (capitalized) replaces the character you type with the character under the cursor. To use these commands, you must be in Normal mode; if you are in Insert mode, press Esc to return to Normal mode. No external software or specific system configuration is required to use these features, as they are built into the standard Vim distribution.
What You'll Need
- A computer running a Linux distribution or another Unix-like operating system.
- The Vim text editor installed. If it is not installed, you can install it using your package manager. For example, on Debian or Ubuntu systems, you would run
sudo apt-get install vim, while on Fedora, you would runsudo dnf install vim. - A text file to edit. You can create a new file with
vim filename.txtor open an existing one.
Step-by-Step
- Open a file in Vim. Launch Vim by typing
vim filename.txtin your terminal. This will open the file in a new window or split screen. If the file is empty, you will see a blank screen with the cursor at the top left. - Ensure you are in Normal mode. By default, Vim opens in Normal mode. You can verify this by looking at the bottom-left corner of the window; it should say "Normal". If you are in Insert mode (where you can type immediately), press
Escto return to Normal mode. - Move the cursor to the target character. Use the arrow keys or Vim's movement commands (like
wfor "word",bfor "beginning of word", or$for the end of the line) to position the cursor exactly over the character you want to replace. For example, if you want to replace the 's' in the word "test", move the cursor so it is blinking over that 's'. - Use the
rcommand to replace a character. Pressrfollowed by the new character you want to insert. The old character under the cursor will be replaced by the new one. For example, if the cursor is over the 's' in "test" and you pressrs, the word will change from "test" to "tess". This is the most common use case: replace a single character with a new one. - Use the
Rcommand to enter overwrite mode. PressR(capitalized). You will notice the cursor changes appearance, often becoming a block or a bold line, and the bottom-left corner of the window will now say "Replace". This enters a state where every key you press will replace the character under the cursor. This is different fromr, which only replaces one character and then immediately returns to Normal mode. You can exit Overwrite mode by pressingEscorCtrl+c. - Practice with the
Rcommand. Let's say you want to change the entire word "test" to "taste". You could userfor each letter, but it's faster to useR. First, move the cursor to the 't' at the start of the word. PressRto enter overwrite mode. Type "taste". The characters "test" will be overwritten one by one as you type. When you finish, pressEscto return to Normal mode. - Undoing a mistake. If you make a mistake while using
rorR, you can undo it by pressingu(lowercase) to undo the last action. This will restore the character to its original state. You can repeatuto undo multiple actions in a row. - Exiting Vim. After you are finished editing, save your changes by typing
:wand pressing Enter. Then, exit Vim by typing:qand pressing Enter. If you have made changes that you want to discard, you can exit without saving by typing:q!and pressing Enter.
Common Mistakes
- Being in the wrong mode. The most common mistake is trying to use
rorRwhile in Insert mode. In Insert mode, Vim is designed for typing new text, not replacing existing characters. If you are in Insert mode, thercommand will not work as expected. Always ensure you are in Normal mode by pressingEscfirst. - Confusing
randR. Remember thatris for replacing a single character, whileRis for entering a continuous overwrite mode. If you pressRand then type a few characters, you will be in overwrite mode until you pressEsc. This can be confusing if you just wanted to replace one character. - Not moving the cursor first. The
rcommand requires you to position the cursor over the character you want to replace before you pressr. If you pressrwithout moving the cursor, Vim will use the character currently under the cursor. This might not be the character you intended to replace. - Forgetting to exit overwrite mode. If you use
Rto replace multiple characters, you must remember to exit overwrite mode by pressingEsc. If you don't, the next key you press will be treated as an overwrite command, which might not be what you want.
Tips
- Combine
rwith movement commands. You can combinerwith movement commands to replace multiple characters in a single line. For example,3rwill replace the current character and the next two characters with the same new character.wwill move to the next word, andrwill replace the first character of that word. This can be a fast way to fix typos in multiple places. - Use
.to repeat the last change. After you userorRto replace a character, you can press.to repeat that change. This is useful if you want to replace the same character multiple times in a row. For example, if you replace a 's' with a 't', you can press.to replace the next 's' with a 't'. - Use
Ctrl+rto redo a change. If you undo a change by pressingu, you can redo it by pressingCtrl+r. This is useful if you change your mind about an undo. - Use
:sfor bulk replacements. If you need to replace multiple characters in a file, therandRcommands might be too slow. The:scommand is a powerful tool for bulk replacements. For example,:s/old/new/gwill replace all occurrences of "old" with "new" in the current line. This is a more efficient way to make global changes.
Troubleshooting
If the r or R command does not seem to be working, check the following common causes. The goal is to identify whether the issue is mode-related, configuration-related, or a misunderstanding of the command's behavior.
- Verify you are in Normal mode. This is the most likely culprit. If you are in Insert mode,
randRare not available for character replacement. PressEscto ensure you are in Normal mode. If you are still unsure, type:set mode?and press Enter. This will display the current mode in the status line at the bottom of the Vim window. - Ensure the file is writable. If you are trying to edit a file that is not writable (for example, if it is a system file located in a protected directory), Vim will not allow you to make changes, even if you use
randR. You will see an error message like "E212: Can't open file for writing" in the status line. To fix this, try usingsudoto run Vim with administrator privileges. For example,sudo vim filename.txt. Be aware that this has security implications and should be done with caution. - Check for custom keybindings. Some Vim plugins or custom configurations might alter the default behavior of the
rorRkeys. If you have recently installed a Vim plugin or configuration, try disabling it temporarily to see if the problem goes away. You can do this by opening your Vim configuration file (usually~/.vimrcor~/.config/nvim/init.vim) and commenting out the relevant lines. - Consider the
compatibleoption. Vim has acompatibleoption that can affect the behavior of certain commands. If this option is set, Vim will try to mimic the behavior of the old Vi editor. This can sometimes cause unexpected behavior with therandRcommands. You can check the status of this option by typing:set compatible?and pressing Enter. If it says "compatible", try typing:set nocompatibleand pressing Enter. This will switch Vim to a more modern mode. - Reset Vim to default settings. If you have made significant changes to your Vim configuration and are still experiencing issues, you can try resetting Vim to its default settings. This will delete any custom configurations you have made. You can do this by typing
:source $VIMRUNTIME/defaults.vimand pressing Enter. This will reload the default configuration file.
Frequently Asked Questions
What is the difference between r and R in Vim?
The primary difference lies in the duration of the replacement. The lowercase r command stands for "replace character". It replaces the character currently under the cursor with the one you type and then immediately returns you to Normal mode. The uppercase R command enters "Replace mode". In this mode, Vim remains in Replace mode until you manually leave it (usually by pressing Esc or Ctrl + c). This means you can type multiple characters to overwrite a longer sequence of text continuously.
Can I replace multiple characters at once with r?
Yes, you can. Vim allows you to prefix the r command with a number to specify how many characters to replace. For example, if you want to replace the current character and the next three characters with the letter 'a', you would type 4ra. Vim will overwrite the current character and the following three characters with 'a', then return you to Normal mode.
How do I undo a replacement made with r or R?
To undo the most recent change (including a single character replacement), press the lowercase u key. If you need to undo multiple replacements in a row, you can press u repeatedly. To redo an action that you have undone, press Ctrl + r.
Why isn't my r command working?
The most common reason is that you are not in Normal mode. In Insert mode, Vim is designed for typing new text, and the r command is not available. Ensure you press Esc to return to Normal mode before attempting to use r. Another possibility is that you are trying to edit a file that is read-only or located in a protected directory.
Is there a way to replace all occurrences of a character in a file?
Yes, the r and R commands are designed for single character replacements. For bulk replacements of text strings, it is more efficient to use the global search and replace command. This is accessed by typing : followed by s (for substitute) and pressing Enter. For example, to replace every instance of the word "foo" with "bar" in the entire file, you would type :%s/foo/bar/g and press Enter. The % signifies the entire file, and the g signifies global replacement.
Can I use r to replace a character with a special character like a newline or tab?
Generally, no. The r command expects a standard printable character. Attempting to replace a character with a newline or tab using r will usually result in an error or unexpected behavior. For inserting new lines or tabs, you should use the standard Insert mode commands or specific Vim commands like o (open a new line below) or I (insert at the beginning of the line).