Efficient changes

3.7
(475)

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 are 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
  • dapD elete A round P aragraph
  • vawV 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

How useful was this tip?

Average rating 3.7 / 5. Vote count: 475

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