Efficient changes

Use text objects to make changes more efficiently

In Vim, you can change inside surrounding characters or change around surrounding characters with ease. Given a method call with a parameter:

some_method("Some nifty string")

We want to replace the nifty static string with a variable:

some_method(snazzy_variable)

One approach would be to place your cursor on the first double quote and remove characters with x until the string is gone. After entering insert mode with i, you can type your replacement, snazzy_variable . But that’s a lot of keystrokes.

Another, more efficient, approach is to use “change inside.” By placing the cursor anywhere inside the parenthesis, and hitting ci( the stating string will disappear and Vim will enter insert mode, leaving it ready to accept snazzy_variable as a replacement.

There a number of useful variations available. Try to remember them with these mnemonics:

  • da” - D elete A round double quotes

  • di] - D elete I nside square brackets

  • ci{ - C hange I nside curly braces

  • dap - D elete A round P aragraph

  • vaw - V isually select A round W ord

As you can see, these can be combined for quite a bit of power and efficiency. For more information, check out Vim help topics: :help text-objects


Was this useful? Help us improve!

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