How to select between brackets (or quotes or ...) in Vim?
Solution 1
Use whatever navigation key you want to get inside the parentheses, then you can use either yi(
or yi)
to copy everything within the matching parens. This also works with square brackets (e.g. yi]
) and curly braces. In addition to y
, you can also delete or change text (e.g. ci)
, di]
).
I tried this with double and single-quotes and it appears to work there as well. For your data, I do:
write (*, '(a)') 'Computed solution coefficients:'
Move cursor to the C
, then type yi'
. Move the cursor to a blank line, hit p
, and get
Computed solution coefficients:
As CMS noted, this works for visual mode selection as well - just use vi)
, vi}
, vi'
, etc.
Solution 2
To select between the single quotes I usually do a vi'
("select inner single quotes").
Inside a parenthesis block, I use vib
("select inner block")
Inside a curly braces block you can use viB
("capital B")
To make the selections "inclusive" (select also the quotes, parenthesis or braces) you can use a
instead of i
.
You can read more about the Text object selections on the manual, or :help text-objects
within vim.
Solution 3
This method of selection is built-in and well covered in the Vim help. It covers XML tags and more.
See :help text-objects
.
Solution 4
For selecting within single quotes use vi'
.
For selecting within parenthesis use vi(
.
Solution 5
Use arrows or hjkl
to get to one of the bracketing expressions, then v
to select visual (i.e. selecting) mode, then %
to jump to the other bracket.
Related videos on Youtube
darjab
Updated on December 23, 2021Comments
-
darjab over 2 years
I'm sure there used to be a plugin for this kinda stuff, but now that I need it, I can't seem to find it (naturally), so I'll just ask nice and simple.
What is the easiest way to select between brackets, or quotes, or generally a list of matching characters?
write ( *, '(a)' ) 'Computed solution coefficients:'
For example, here I'd like to select
(a)
, orComputed solution coefficients:
.I'm not interested in multiline, just cases which occur on one line.
-
darjab almost 15 yearsI'm generally looking for a way to select everything between predefined matching chars (normally only single and double quotes, and brackets of all kinds).
-
Tim Whitcomb almost 15 yearsWhat do you mean by separated words? I tried it on "[x, y, z]" and it picked out "x, y, z"
-
Stobor almost 15 years:help text-objects gets closer to the useful stuff... you could at least mention some of the common ones, like a" and a( when in visual mode.
-
Stobor almost 15 years(Also, +1 because despite being a long-time vim user, I didn't know about these!)
-
darjab almost 15 yearsDisregard the last comment - found what the problem was. Works like a charm :-)
-
michael almost 15 years@Stobor . Cheers fixed that reference
-
alfredodeza over 13 yearsthanks for that tip, I have been searching and asking for something similar and wasn't satisfied with my results. Awesome.
-
Ghazaly over 13 yearsthis tip is very simple and easy to commit inside my brain and muscle memory. thanks.
-
Nubzor about 12 yearsit seems to work with quotes as well
yi"
select all withing quotes -
pyronaur about 11 years
ci(
orci)
does it for what I was looking for. Thanks. I still don't fully understand thei
in that case, but who cares as long as it works, right ? :) -
pyronaur about 11 yearsNote:
ci'
orci(
for changing instead of visually selecting. But it does exactly what I wanted. Thanks :) -
Tim Whitcomb about 11 yearsAs noted below, check the help for text-objects. the
i
is modifying thec
command, so it's no longer "insert": I think of it as "inside" soci(
becomes "change inside parentheses". If you want to include the parens, usea
instead ofi
likeca(
-
Anton Beloglazov about 10 yearsI've added an answer below describing my plugin
vim-textobj-quotes
that provides a unified text object for handling different type of quotes with a single key binding. -
Rai over 9 yearsThis is a highly useful tip. No more fumbling with extra keystrokes. Thank you for the link.
-
Moj over 8 yearsis there any method that after yanking it goes to the insert mode?
-
816-8055 over 7 yearsThe second one only works when within that () block, so usually I do
%vi(
or%vib
-
aturegano over 7 yearsNote that it is not needed to be inside the quotes when you call it, the own command looks for the first occurence of text inside quotes in the current line and deletes it, so you can save some key strokes.
-
Tim Whitcomb over 7 yearsNeat! That's awesome - however, note that it only works with quotes. Trying it with the parentheses didn't seem to work.
-
pipe over 5 yearsThis is completely orthogonal to the problem and should not be an answer. You can write this on every question which asks about
y
. -
yuriploc over 5 yearsThis is an addition to the answer, not an answer itself.
-
pipe over 5 yearsThen it should be an edit to that answer, this post as-is does not answer the question.
-
Paul Parker over 5 yearsThis is an advertisement for NOT using gvim :) Who needs an extra two shift+keypresses in their life?
-
Eric Burel over 2 yearsDoesn't seem to work very well with azerty (iso-fr) layout, because {, [, has to be accessed using the "Alt Gr" key.