Vim tip 11: replace characters in Normal mode
Often, you just need to change one character. For example, changing i
to j
, 2
to 4
, '
to "
and so on.
- rj replace the character under the cursor with
j
- ry replace the character under the cursor with
y
- 3ra replace the character under cursor as well as the two characters to the right with
aaa
- no changes will be made if there aren't sufficient characters to match
To replace multiple characters with different characters, use R
.
- Rlion followed by Esc replace the character under cursor and three characters to the right with
lion
- Esc key marks the completion of
R
command - Backspace key will act as an undo command to give back the character that was replaced
- if you are replacing at the end of a line, the line will be automatically extended if needed
- Esc key marks the completion of
The advantage of r
and R
commands is that you remain in the Normal mode, without needing to switch to Insert mode and back.
Video demo:
See also my Vim Reference Guide and curated list of resources for Vim.