tags:

views:

2931

answers:

7

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 lay on one line.

+9  A: 

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]).

edit: 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:

edit: as CMS noted, this works for visual mode selection as well - just use vi), vi}, vi', etc.

Tim Whitcomb
Doesn't work with separated words.
ldigas
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).
ldigas
What do you mean by separated words? I tried it on "[x, y, z]" and it picked out "x, y, z"
Tim Whitcomb
I updated the question - the "Computed ..." part in bold.
ldigas
Thanks - updated answer w/ your specific example.
Tim Whitcomb
Disregard the last comment - found what the problem was. Works like a charm :-)
ldigas
+4  A: 

This method of selection is in built and well covered in the vim help. It covers xml tags and more

:help text-objects

michael
: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
(Also, +1 because despite being a long-time vim user, I didn't know about these!)
Stobor
@Stobor . Cheers fixed that reference
michael
+2  A: 

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

Stobor
(doesn't do quotes, unfortunately, though...)
Stobor
+1  A: 

Write a vim function in .vimrc using the searchpair builting 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)

Adrian Panasiuk
+9  A: 

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.

CMS
+1  A: 

For selecting within single quotes use vi' For selecting within parans use vi(

Canopus
A: 

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.