Search code examples
vimquotesbracketsparenthesesyank

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


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.


Solution

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