Vim's Change, Replace & Substitute

Change, Substitute, & Replace…

Read this after you’ve familiarized yourself with Delete, Yank, and Put (Cut, Copy, And Paste)

Change

Change (c) is similar to Delete. The difference is that Delete tells Vim “I want to get rid of this and go away.” Change says “I want to get rid of this and replace it with something else.” The practical difference is that after telling Vim what you want to change, it puts you into Insert mode so that you can start entering the replacement text. Both take the removed stuff and place it in the same buffer for pasting later.

Change is one of my favorite little helpers in Vim, but it’s mostly useful when combined with movement commands.

An example:

she sells sea shells

Let’s change “sea shells” to “hats”. With my cursor on the “s” in “sea” I have a few options for selecting the text to delete before jumping into insert mode and typing “hats”. To do it were going to focus on matching the first thing after the bit we want to get rid of. In this case it’s the word “by”, it’s “b” , or the space before the “b”.

  ctb        " Change up To the "b" in "by".
  c/b        " Change to the first match of my search (for the letter b)
  c/by       " Same thing, but I searched for more.
  c$ or C    " Change all remaning characters on the line.
  cfb        " Easy, but eats the b so you'll have to type it again.
  2ct<SPACE> " Change up To the 2nd space character
  2cf<SPACE> " same thing but grabs the 2nd space character too.
  11c→       " the "b" is 11 characters down the line.
  11cl       " using l to move right instead of →
             "  there are very few cases where these last two are useful.

I use the first 2 constantly. I don’t think I’ve ever used the ones that involve counting except to show how it could be done. Mostly I use c with a search because it works in the same line and across multiple lines.

There’s one more thing to know about change. To change everything to the end of the line you use c$ or Captial C C. See “End Of Line” below for details.

Delete

You can do intra-line deletions as well. They work exactly the same as change except that they don’t switch into insert mode after they delete the characters.

Substitute

Substitute (s) is like change, only less useful. It deletes whatever is under the cursor or [visually selected]((/2019/03/03/vims-visual-mode/). It stashes it away for later pasting like yank, and then drops you into insert mode. You can preface it with a number like 2s and it will delete that many characters before switching into insert mode. I think counting characters is effing obnoxious when I could just use c so I never really use this.

Replace

Replace is a little funky. With nothing selected it replaces the character under the cursor with whatever character you type next. The funky bit is that if you make a [visuall selection]((/2019/03/03/vims-visual-mode/) it will replace every character in that selection with whatever character you type next. I can think of a few uses for this, but it’s not too common.

End Of Line

$ means “end of line” in searches. The Change and Delete commands described above interact with it a little differently. They assume that the character you type after the t or f is the literal character you want it to consider the end. So ct$ means change to the next dollar sign character.

If you combine c or d with search (e.g. c/$) the $ still means end of line. However, there’s a gotcha. Most programmers think of the newline character as the end of the line. Vim thinks that the last visible character is the end of the line. So in the sentence, “She sells sea shells.” the period is the end of line and c/$ changes everything from your starting point up to the period.

Practically speaking this means if you want to change to the end of the line the easiest way is c$ or C. I use c$ because I use lower case c for all the other things. It’s less weird to me to remember “leave off the t” than it is to say “oh yeah this one is capital C”.