VimTrick: 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 - delete to the next c

  • ci} - change inside the curly braces

  • cf, - change forward to and include the comma

  • di” - delete inside the quotes. This does not delete the quotes.

  • ya’ - yank around 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 (delete, change, yank) can be combined with (to, forward, inside, and around) 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: