Adding parenthesis around highlighted text in Vim

In addtition to Dmitry's suggestion of the surround plugin, adding parenthesis around highlighted text can be done with the following command:

xi()<Esc>P

You can set a map in visual mode using (for example) \s by adding the following to your ~/.vimrc file:

xnoremap <leader>s xi()<Esc>P

You need an awesome and must-have plugin surround. Then, it will happily do what you want if you select text and type Sb (surround-braces), or S) (note the capital S !).

It actually can do a lot of surrounding: various quotes, tags, etc. It allows you to put cursor in the double-quoted word and change double quotes to single quotes by typing: cs"' (change-surround " to '). Or you can completely delete quotes by typing ds" (delete-surround ").

Read the docs by link, it is really awesome!


The comannd

c()<Esc>P

Explanation

If you want to put the word under the cursor in to brackets this is viwc()<Esc>P.

viw will visually select all charactrs in a word.

c() will cchange the selection and by dropping you into insert mode, where you type the characters ( ). c automatically copies the original content your yank buffer (clip board).

With <Esc>P you return Escback from insert to normal mode and Paste the previous content.

Tags:

Vim