Quick Replacements

Go easy on your x key with these efficient combinations.

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.


Was this useful? Help us improve!

With your feedback, we can improve VimTricks. Click a link to vote: