Enclosing in parentheses with Vim

eqb picture eqb · Nov 9, 2011 · Viewed 43.2k times · Source

Is there a way to enclose some text in parentheses (or curly brackets) in vim?

In other words, how would you do this?

Initial string:

It is sunny outside.

Final string:

It is (sunny) outside.

On a funny note, I just hit :w to submit this question.

Happy vim-ing, SOCommunity!

Answer

Ian Hatch picture Ian Hatch · Jul 11, 2014

For one word, I've been using:

bcw()<Esc>P

That is, go to the start of the word, cut the word and go into insert mode, type the brackets and then exit insert mode and paste the word back in.

Keep in mind that this will overwrite your yank register.

You can, of course replace bcw with whatever movement and selection types you need, like

5cc{<Enter>}<Esc>P