Vim - select/yank/delete content between brackets including brackets

34

13

In vim, is it possible to select content between the brackets inclusive of the brackets similar to

yi{ yi(

But I want to include the brackets.

Hamish

Posted 2011-05-10T23:22:13.587

Reputation: 547

Answers

1

You can move the cursor over one bracket, and do vf{ or vf( to bring you into visual mode and then select everything until (and including) the bracket.

Wuffers

Posted 2011-05-10T23:22:13.587

Reputation: 16 645

5Actually, this will end up selecting only the opening brace. If you're on top of the opening brace, f{ will move you to the closest opening brace, which you're already on. What you probably want is v% which will move you to the matching brace and can be used on either side. – Michael Mior – 2014-11-23T05:32:41.627

1This is not the correct answer. See @garyjohn's answer below. – Michael Sandman – 2019-05-13T14:49:56.163

78

Yes. Use a instead of i, as

ya{
ya(

See

:help a{
:help a(

and more generally,

:help text-objects
:help 04.8

garyjohn

Posted 2011-05-10T23:22:13.587

Reputation: 29 085

3This should be the accepted answer! – phanhuy152 – 2018-07-09T07:36:09.457

very convenient! :) – kmario23 – 2019-12-18T13:16:40.207

3

Does f{v% or f(v% do what you want? It moves your cursor to the next { or (, enters you into visual mode, and then moves your cursor to the corresponding closing } or ). If you're already past the scope you want to select, you can use a capital F. Works just as well to jump to the closing } or ) first, too -- f}v%.

Once you have what you want selected, you can y, d, x, etc. it. The % command works multi-line, too, so you can use this technique on large blocks of code if you wish (although f and F do not, so you have to start on either the first or last line).

EDIT: Better answer, seems to be exactly what you're looking for:

ya(

Replacing the i in your original command with a does exactly the same thing, except that it includes the '(' character. This is "yanking a block", whereas yi( is "yanking an inner block".

Kromey

Posted 2011-05-10T23:22:13.587

Reputation: 4 377

1

One another way would be by following the below steps:

  1. place the cursor on the opening parenthesis ( or braces {
  2. press esc key and press v to enter into the visual mode
  3. now press the % symbol (this will select the whole text between parens inclusive)
  4. press the key y to yank (i.e. copy) the text (press d if you rather want to cut it.)

Then, you can move the cursor wherever you want the new text to be pasted and then press p for pasting the text there.

kmario23

Posted 2011-05-10T23:22:13.587

Reputation: 113

Super helpful, thank you!! – SamAndrew81 – 2020-01-03T18:56:47.747