How to select between brackets (or quotes or ...) in Vim?
Asked Answered
D

11

282

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), or Computed solution coefficients:.

I'm not interested in multiline, just cases which occur on one line.

Dirt answered 30/6, 2009 at 5:54 Comment(0)
D
316

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.

Dichroite answered 30/6, 2009 at 5:59 Comment(11)
I'm generally looking for a way to select everything between predefined matching chars (normally only single and double quotes, and brackets of all kinds).Dirt
What do you mean by separated words? I tried it on "[x, y, z]" and it picked out "x, y, z"Dichroite
Disregard the last comment - found what the problem was. Works like a charm :-)Dirt
it seems to work with quotes as well yi" select all withing quotesOrose
ci( or ci) does it for what I was looking for. Thanks. I still don't fully understand the i in that case, but who cares as long as it works, right ? :)Subtractive
As noted below, check the help for text-objects. the i is modifying the c command, so it's no longer "insert": I think of it as "inside" so ci( becomes "change inside parentheses". If you want to include the parens, use a instead of i like ca(Dichroite
I'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.Hesson
is there any method that after yanking it goes to the insert mode?Cranwell
Note 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.Rounce
Neat! That's awesome - however, note that it only works with quotes. Trying it with the parentheses didn't seem to work.Dichroite
Doesn't seem to work very well with azerty (iso-fr) layout, because {, [, has to be accessed using the "Alt Gr" key.Wester
M
422

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.

Moonfaced answered 30/6, 2009 at 6:17 Comment(4)
thanks for that tip, I have been searching and asking for something similar and wasn't satisfied with my results. Awesome.Deibel
this tip is very simple and easy to commit inside my brain and muscle memory. thanks.Assortment
This is a highly useful tip. No more fumbling with extra keystrokes. Thank you for the link.Hotchpot
vi" to select inside double quotes "Trumpet
D
316

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.

Dichroite answered 30/6, 2009 at 5:59 Comment(11)
I'm generally looking for a way to select everything between predefined matching chars (normally only single and double quotes, and brackets of all kinds).Dirt
What do you mean by separated words? I tried it on "[x, y, z]" and it picked out "x, y, z"Dichroite
Disregard the last comment - found what the problem was. Works like a charm :-)Dirt
it seems to work with quotes as well yi" select all withing quotesOrose
ci( or ci) does it for what I was looking for. Thanks. I still don't fully understand the i in that case, but who cares as long as it works, right ? :)Subtractive
As noted below, check the help for text-objects. the i is modifying the c command, so it's no longer "insert": I think of it as "inside" so ci( becomes "change inside parentheses". If you want to include the parens, use a instead of i like ca(Dichroite
I'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.Hesson
is there any method that after yanking it goes to the insert mode?Cranwell
Note 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.Rounce
Neat! That's awesome - however, note that it only works with quotes. Trying it with the parentheses didn't seem to work.Dichroite
Doesn't seem to work very well with azerty (iso-fr) layout, because {, [, has to be accessed using the "Alt Gr" key.Wester
K
34

This method of selection is built-in and well covered in the Vim help. It covers XML tags and more.

See :help text-objects.

Keelin answered 30/6, 2009 at 6:4 Comment(3)
: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.Armlet
(Also, +1 because despite being a long-time vim user, I didn't know about these!)Armlet
@Armlet . Cheers fixed that referenceKeelin
B
30

For selecting within single quotes use vi'.

For selecting within parenthesis use vi(.

Bentz answered 30/6, 2009 at 6:24 Comment(2)
Note: ci' or ci( for changing instead of visually selecting. But it does exactly what I wanted. Thanks :)Subtractive
The second one only works when within that () block, so usually I do %vi( or %vibPump
A
11

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.

Armlet answered 30/6, 2009 at 6:4 Comment(1)
(doesn't do quotes, unfortunately, though...)Armlet
C
6

I wanted to add to the already good answers. I came here looking for a way to change the text inside of html brackets, so I want to provide an answer for anyone else that is also looking for that.

You might think ci< would work, but actually that only works if you are inside one of the tags themselves:

<would work inside here> But not here </would work inside here>

What I wanted was to change the text between the html tags themselves:

<div>change me</div>

What I wanted was "change inner tag": cit

Thank you to the other answer that mentioned the documentation (:help text-objects) which is how I found what I was looking for.

Cleome answered 23/12, 2021 at 0:19 Comment(0)
N
6

Stop at the beginning of brackets, and then select all and print:

vi' 

vi(

vi[ 

vi{

vi<

vi"

if you want to delete replace vi with di

Neritic answered 5/1, 2023 at 19:22 Comment(0)
F
3

Write a Vim function in .vimrc using the searchpair built-in function:

searchpair({start}, {middle}, {end} [, {flags} [, {skip}
            [, {stopline} [, {timeout}]]]])
    Search for the match of a nested start-end pair.  This can be
    used to find the "endif" that matches an "if", while other
    if/endif pairs in between are ignored.
    [...]

(http://vimdoc.sourceforge.net/htmldoc/eval.html)

Forbidding answered 30/6, 2009 at 6:15 Comment(0)
A
3

I would add a detail to the most voted answer:

If you're using gvim and want to copy to the clipboard, use

"+<command>

To copy all the content between brackets (or parens or curly brackets)

For example: "+yi} will copy to the clipboard all the content between the curly brackets your cursor is.

Argive answered 9/2, 2017 at 19:48 Comment(4)
This is completely orthogonal to the problem and should not be an answer. You can write this on every question which asks about y.Karriekarry
This is an addition to the answer, not an answer itself.Argive
Then it should be an edit to that answer, this post as-is does not answer the question.Karriekarry
This is an advertisement for NOT using gvim :) Who needs an extra two shift+keypresses in their life?Ratio
H
1

I've made a plugin vim-textobj-quotes: https://github.com/beloglazov/vim-textobj-quotes

It provides text objects for the closest pairs of quotes of any type. Using only iq or aq it allows you to operate on the content of single ('), double ("), or back (`) quotes that currently surround the cursor, are in front of the cursor, or behind (in that order of preference). In other words, it jumps forward or backwards when needed to reach the quotes.

It's easier to understand by looking at examples (the cursor is shown with |):

  1. Before: foo '1, |2, 3' bar; after pressing diq: foo '|' bar
  2. Before: foo| '1, 2, 3' bar; after pressing diq: foo '|' bar
  3. Before: foo '1, 2, 3' |bar; after pressing diq: foo '|' bar
  4. Before: foo '1, |2, 3' bar; after pressing daq: foo | bar
  5. Before: foo| '1, 2, 3' bar; after pressing daq: foo | bar
  6. Before: foo '1, 2, 3' |bar; after pressing daq: foo | bar

The examples above are given for single quotes, the plugin works exactly the same way for double (") and back (`) quotes.

You can also use any other operators: ciq, diq, yiq, viq, etc.

Please have a look at the github page linked above for more details.

Hesson answered 9/4, 2014 at 9:55 Comment(0)
C
-8

A simple keymap in vim would solve this issue. map viq F”lvf”hh This above command maps viq to the keys to search between quotes. Replace " with any character and create your keymaps. Stick this in vimrc during startup and you should be able to use it everytime.

Ciro answered 30/7, 2009 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.