Quick Replacements

3.3
(440)

We’ve been getting a lot of requests for Vim fundamentals: tricks everyone should know but perhaps many might not. So here’s one that’s essential for editing anything at all in Vim, whether it’s a journal, data, or software.

Everyone is aware that x deletes the character under your cursor. But if you’re making a lot of changes this way, you can quickly exact a severe toll on your x key. When changing chunks of text, your efficiency as well as your wear and tear on the keyboard can benefit greatly by using a few actions and motions:

  • dtc – _ d elete t _o the next c
  • ci} – _ c hange i _nside the curly braces
  • cf, – _ c hange f _orward to and include the comma
  • di” – _ d elete i _nside the quotes. This does not delete the quotes.
  • ya’ – _ y ank a _round single quotes. This includes the quotes. (The yanked value can be pasted with p. Try using the clipboard register to paste somewhere else!)

Some obvious mnemonics emerge: d for delete and c for change. The difference between change and delete is simple: Change drops you into insert mode, ready to type replacement text. Delete leaves you in normal mode, so you can perform another edit.

Consider this example in the screencast below, which handles the first two on the list. We have a badly named variable, and we want to replace the contents of a code block in Ruby:

As you can likely see, the various actions (_ d elete, c hange, y ank) can be combined with ( t o, f orward, i nside, and a _round) and a character to act until, (}, _, comma, , or ). When these combinations become muscle memory, much can be accomplished in a few quickly flowing keystrokes.

How useful was this tip?

Average rating 3.3 / 5. Vote count: 440

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Andy Libby

36 Posts

Rider of bicycles. Writer of code. User of Vim.
View all posts