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”
- Delete Around double quotesdi]
- Delete Inside square bracketsci{
- Change Inside curly bracesdap
- Delete Around Paragraphvaw
- Visually select Around Word
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:
Instead of ( or { it is possible to use b or B respectively, which for qwerty users is a bit easier to type imo, I use it all the time. (this info is of course in :h text-objects)
Also definitely check out https://github.com/wellle/targets.vim for more text-objects especially to deal with (function) arguments, or I use it a lot to edit snake_case_variable_names (e.g. da_ will delete one word with one _), and much much more..