Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enclosing in parentheses with Vim

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!

like image 482
eqb Avatar asked Nov 09 '11 20:11

eqb


People also ask

How do you add parentheses in Vim?

lh-brackets simply binds ( to surround the selection with the brackets. Unlike surround it doesn't follow the vim usual keybinding philosophy as does. Instead less keys are required.


1 Answers

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 
like image 154
Ian Hatch Avatar answered Oct 02 '22 18:10

Ian Hatch