Replace and substitute with r and s

Part of my guide of vim tips/snippets

Learn how to replace and substitute text

Replacing characters / a few characters is a common task when using Vim. Here are some ways to achieve that.

Replace with r (returns you back to command mode)

If you have your cursor on a character in a word, and you want to replace it, you can press r followed by the character to replace it with.

Note: you can enter a new line under the cursor with r followed by the enter key (replacing it with a new line)

Replace multiple times with R (leaves you in replace mode)

If you type Rabcd<Esc> then it will replace the next 4 characters with abcd. Press esc to quit this mode.

Substitute with s (keeps you in insert mode)

If you want to replace the current characte, and continue typing (in insert mode), press s

(This is similar to cl. )

You can also replace a bunch of characters together, e.g. 3s will remove 3 characters, and put you in insert mode so you can type new characters.

Change text with c

Use something like cw (c for change, + the motion of w for word) will delete the rest of the word and put you in insert mode so you can type.

cl (c with the motion of l for letter) is basically the same as pressing s

If your cursor is in the middle of a word, press ciw to replace the entire word (c + iw for inner word (whole word))

Part of my guide of vim tips/snippets