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 run sudo dnf install vim.
  • A text file to edit. You can create a new file with vim filename.txt or open an existing one.

Step-by-Step

  1. Open a file in Vim. Launch Vim by typing vim filename.txt in 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.
  2. 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 Esc to return to Normal mode.
  3. Move the cursor to the target character. Use the arrow keys or Vim's movement commands (like w for "word", b for "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'.
  4. Use the r command to replace a character. Press r followed 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 press rs, the word will change from "test" to "tess". This is the most common use case: replace a single character with a new one.
  5. Use the R command to enter overwrite mode. Press R (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 from r, which only replaces one character and then immediately returns to Normal mode. You can exit Overwrite mode by pressing Esc or Ctrl + c.
  6. Practice with the R command. Let's say you want to change the entire word "test" to "taste". You could use r for each letter, but it's faster to use R. First, move the cursor to the 't' at the start of the word. Press R to enter overwrite mode. Type "taste". The characters "test" will be overwritten one by one as you type. When you finish, press Esc to return to Normal mode.
  7. Undoing a mistake. If you make a mistake while using r or R, you can undo it by pressing u (lowercase) to undo the last action. This will restore the character to its original state. You can repeat u to undo multiple actions in a row.
  8. Exiting Vim. After you are finished editing, save your changes by typing :w and pressing Enter. Then, exit Vim by typing :q and 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 r or R while in Insert mode. In Insert mode, Vim is designed for typing new text, not replacing existing characters. If you are in Insert mode, the r command will not work as expected. Always ensure you are in Normal mode by pressing Esc first.
  • Confusing r and R. Remember that r is for replacing a single character, while R is for entering a continuous overwrite mode. If you press R and then type a few characters, you will be in overwrite mode until you press Esc. This can be confusing if you just wanted to replace one character.
  • Not moving the cursor first. The r command requires you to position the cursor over the character you want to replace before you press r. If you press r without 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 R to replace multiple characters, you must remember to exit overwrite mode by pressing Esc. 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 r with movement commands. You can combine r with movement commands to replace multiple characters in a single line. For example, 3r will replace the current character and the next two characters with the same new character. w will move to the next word, and r will 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 use r or R to 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 + r to redo a change. If you undo a change by pressing u, you can redo it by pressing Ctrl + r. This is useful if you change your mind about an undo.
  • Use :s for bulk replacements. If you need to replace multiple characters in a file, the r and R commands might be too slow. The :s command is a powerful tool for bulk replacements. For example, :s/old/new/g will 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, r and R are not available for character replacement. Press Esc to 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 r and R. You will see an error message like "E212: Can't open file for writing" in the status line. To fix this, try using sudo to 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 r or R keys. 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 ~/.vimrc or ~/.config/nvim/init.vim) and commenting out the relevant lines.
  • Consider the compatible option. Vim has a compatible option 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 the r and R commands. You can check the status of this option by typing :set compatible? and pressing Enter. If it says "compatible", try typing :set nocompatible and 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.vim and 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).