What is the difference between s, c and r commands in vi/vim?


s (substitute) will delete the current character and place the user in insert mode with the cursor between the two surrounding characters. 3s, for example, will delete the next three characters and place the user in insert mode.

c (change) takes a vi/vim motion (such as w, j, b, etc.). It deletes the characters from the current cursor position up to the end of the movement. Note that this means that s is equivalent to cl (vim documentation itself claims these are synonyms).

r (replace) never enters insert mode at all. Instead, it expects another character, which it will then use to replace the character currently under the cursor.


Take your sample text, and image the cursor at the beginning of the word 'can' (line 3).

Typing spl<Esc> in vi/vim.

Here, we have s for substitute. pl is the text to insert, and <Esc> will exit insert mode. Put together, it will change can to plan like this:

With a
screen editor,
you plan
scroll the page, move the cursor.

Typing cwcould<Esc> in vi/vim.

c is for change, and w tells c to delete through the next word, can. Next we type the text for insert mode, could. Lastly, we need to type <Esc> again to exit insert mode. The command will change can to could like this:

With a
screen editor,
you could
scroll the page, move the cursor.

Typing rf in vi/vim.

Here we type r for replace, then f as the new character which r uses to replace the original character with. This changes can to fan like this:

With a
screen editor,
you fan
scroll the page, move the cursor.


There's a lot of hidden uses to the simple commands in vi/vim that highlight more differences between these commands. Since I almost always use vim over vi, these features might be vim-exclusive, I'm not certain.

Maximizing the utility of commands like c, y, and d that take motions requires having a good grasp of text-objects (type help text-objects in vim. These aren't in vi.)

Because r takes a character instead of entering insert mode, you can input characters that would otherwise be difficult to add in. Typing r<C-R> (that's r, then ctrl-r) replaces the current character with a ctrl-r character. This might be surprising since pressing ctrl-r in insert mode awaits another key that is the register to paste.

All three of these commands can be repeated with the . command, substituting, changing, or replacing the same region relative to the cursor with the given text.

When typing a number n before the command, s and c delete n items (characters for s, or movements for c), and then inserts text once. Using a number n before r, however, replaces the next n characters with that many copies of the chosen character. For example, 4r0 could replace 1234 with 0000 all at once.

Assume you have foo in the document, and the cursor is on the f.

Now, pressing rb will change this to boo, and you are back in command mode. Pressing sb will accomplish the same, but you are in insert mode and can insert more characters. Finally, c requires some kind of motion; e.g. you can type cw to remove the whole word and enter insert mode. On the other hand, cl is essentially the same as s.