Efficient changes
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 quotesdi]
– D elete I nside square bracketsci{
– C hange I nside curly bracesdap
– D elete A round P aragraphvaw
– 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