Adding parenthesis around highlighted text in Vim

14

3

Is there a functionality in Vim that allows parenthesis to be added around the highlighted text?

For instance, if I highlight n = getchar() in if (n = getchar() == '\n'), I would want to put parenthesis around that.

mrQWERTY

Posted 2015-02-08T00:26:00.963

Reputation: 271

Answers

18

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!

Dmitry Frank

Posted 2015-02-08T00:26:00.963

Reputation: 699

1You didn't explain how to do it with the selection, though. I got led here because it wasn't immediately obvious. – JackHasaKeyboard – 2016-08-05T13:24:13.990

@JackHasaKeyboard, what do you mean I didn't explain how to do it with the selection? The second sentense in my answer says:

Then, it will happily do what you want if you select text and type sb (surround-braces), or s).

– Dmitry Frank – 2016-08-05T14:31:21.340

1So you did. Not working for me though, hitting s just deletes the selection and enters insert mode. – JackHasaKeyboard – 2016-08-05T14:43:13.710

1It means that the "surround" plugin I mentioned in my answer is not correctly installed. – Dmitry Frank – 2016-08-05T14:51:51.790

Awsome indeed. I've been using it for a while and can bet. – biocyberman – 2017-03-14T10:42:21.253

no, you do not need a plugin... – bx2 – 2017-06-16T14:42:11.667

20

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

dotancohen

Posted 2015-02-08T00:26:00.963

Reputation: 9 798

1This looks like a nice and simple solution. If I don't find any issues with this in the future, I'll prefer this over additional plugins. Thanks! – Zelphir Kaltstahl – 2016-11-13T18:05:46.113

3It's a good solution, but not quite as complete as using the surround plugin as it doesn't work if you are selecting whole lines (using V instead of v). In this case it just pastes the lines below your parens. – Rohan Orton – 2017-10-09T18:04:36.820

1

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.

Otherwise, there are many ways to proceed. If you don't mind messing the unnamed register, you also use s(^R")<esc> (^R like CTRL-R)

Luc Hermitte

Posted 2015-02-08T00:26:00.963

Reputation: 1 575

0

Building on dotancohens answer, I put the following in my .vimrc:

xnoremap <leader>( <ESC>`>a)<ESC>`<i(<ESC>

You can easily make similar mappings for [], {}, etc. It works by jumping to the start and end markers implicitly set after ending visual mode. This way selecting whole lines will add the parens at the start/end of the first/last line; it won't overwrite your yank register; and it'll leave the cursor right before the opening paren.

Vir

Posted 2015-02-08T00:26:00.963

Reputation: 101