44

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.

5 Answers 5

0

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.

2
  • 6
    Actually, 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. Nov 23, 2014 at 5:32
  • 2
    This is not the correct answer. See @garyjohn's answer below. May 13, 2019 at 14:49
110

Yes. Use a instead of i, as

ya{
ya(

See

:help a{
:help a(

and more generally,

:help text-objects
:help 04.8
4
  • 5
    This should be the accepted answer!
    – phanhuy152
    Jul 9, 2018 at 7:36
  • 2
    very convenient! :)
    – kmario23
    Dec 18, 2019 at 13:16
  • Some blocks end with a comma, and this command do not copy the entire line. Do you know a way to copy it all? For example in lua you can have a block like { { name = "steam-turbine", craft-time = 3, }, { name = "boiler", craft-time =2, }, }, (since this is a comment, I cant put one in each line, but imagine the block as one command per line). Now the last line of a block is }, and it would be useful to copy both chars.
    – DrBeco
    Jan 3 at 21:52
  • What I would do is first select the block linewise with Va{, then yank the selected text with y. This is documented in :help v_object-select, but I don't think it's very clear.
    – garyjohn
    Jan 4 at 22:49
5

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".

0
5

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.

1
  • Super helpful, thank you!! Jan 3, 2020 at 18:56
3

I use vi{ or va{ to select the content between brackets. The advantage is that you will be able to see the selection before you yank it.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 3, 2022 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .