select text between brackets or quotes

146 views
Skip to first unread message

gabor

unread,
Sep 1, 2007, 10:04:39 AM9/1/07
to vim_use
hi,

what is the simplest way to select the text between two brackets
(parentheses, quotes) so that the selection does not include the
brackets?

basically i want to replace the content of the brackets while the
cursor is located anywher inside. i can use the command %v% to make a
selection including the brackets, however i'd like to exclude them.

another question is how to do the same for quoted strings?

cheers,
gabor

Franco Saliola

unread,
Sep 1, 2007, 10:10:05 AM9/1/07
to vim...@googlegroups.com
On 9/1/07, gabor <gabor....@gmail.com> wrote:

> what is the simplest way to select the text between two brackets
> (parentheses, quotes) so that the selection does not include the
> brackets?

Probably

ci)

and

ci"

Read this as "(c)hange (i)nner )". There is also ciw for change inner word.

And you can replace the c with a d for delete.

Franco

--

Tim Chase

unread,
Sep 1, 2007, 10:12:43 AM9/1/07
to vim...@googlegroups.com
> what is the simplest way to select the text between two brackets
> (parentheses, quotes) so that the selection does not include the
> brackets?


Sounds like you want to use text objects:

:help text-objects

which allow you to do things like

vi[

to "go Visual with the Inner contents of the '['". This works
anywhere within the "[...]" region (including on the brackets
themselves).

Since vim7, there are now "quote" text objects too (yay! a
feature I requested and got :)

vi"

and

vi'

(though the quote versions aren't multi-line like the [/(/{
versions are, which occasionally bites me when writing python
code and using multi-line """...""" strings, but I've learned
just not to expect that to work yet)

Since text-objects are pseudo-motions, they can be applied
directly to many objects, so if you want to yank the contents of
the "[...]", you can "yi["; if you want to fold the current
multi-line "{...}" block, you can "zfi{", and if you want to
change the contents of the current string, you can use

ci"

There are also "outer" variants using "a" instead of "i" which
include the outer characters rather than excluding them. This
may also end up including leading whitespace.

Hope this helps,

-tim


Tony Mechelynck

unread,
Sep 1, 2007, 10:22:29 AM9/1/07
to vim...@googlegroups.com

What you need is object selection:

see
:help i(
:help i[
:help i{
:help i<
:help i"
:help i'

The latter two only work within one line; the rest can span line breaks.

For instance, vi" visually selects the inner double-quoted string, excluding
the quotes.


Best regards,
Tony.
--
Good day to avoid cops. Crawl to school.

gabor

unread,
Sep 1, 2007, 10:48:57 AM9/1/07
to vim_use
cool! this is exactly what i was looking for.

thanks to all of you for the elaborate answers.
good to see that vim has such a helpful community.

gabor

Dave Land

unread,
Sep 1, 2007, 1:15:26 PM9/1/07
to vim...@googlegroups.com
On Sep 1, 2007, at 7:48 AM, gabor wrote:

> cool! this is exactly what i was looking for.
>
> thanks to all of you for the elaborate answers.
> good to see that vim has such a helpful community.

This discussion also reminded me of a very handy mapping that some
here may enjoy...

I do lots of HTML (actually, JSP) editing in my work, and so frequently
have need to select the "innerHTML" of an pair of tags. I have installed
matchit.vim(1) and set the following in my .vimrc:

let b:match_words='<\([\:0-9A-Za-z_-]\+\)[^>]*>:<\/\1[^>]*>,<%:%>'

That match_words string works nearly all of the time for HTML, XML, and
so forth, but occasionally fails if the capitalization of the tags does
not match -- it'll match forward, but not backwards.

Then, I set the following mapping (I use a Mac, so I chose option-A
because it is similar to command-A, which is "select all"):

map å Vk%k

I put the cursor on the first line of the "innerHTML" I want to select,
then use the mapping, which goes up to the opening tag, uses % to find
the appropriate closing tag, then goes up again to deselect the closing
tag. Voilá -- I've selected the contents of a pair of tags.

Dave

(1) http://www.vim.org/scripts/script.php?script_id=39

Tony Mechelynck

unread,
Sep 1, 2007, 1:39:21 PM9/1/07
to vim...@googlegroups.com


The above mapping (using V = linewise-visual) seems to imply that each HTML
tag is always alone on its line. What about

some text with <b>bold,</b> <i>italic or <b>both</b></i> on <u>the same line</u>?

I guess you would have to adjust characterwise... let's see... what about

:map <F5> vF>h%F<lt>?.<CR>of>/.<CR>
?


Best regards,
Tony.
--
"I have to convince you, or at least snow you ..."
-- Prof. Romas Aleliunas, CS 435

Jürgen Krämer

unread,
Sep 2, 2007, 6:13:48 AM9/2/07
to vim...@googlegroups.com

Hi,

Dave Land wrote:
>
> This discussion also reminded me of a very handy mapping that some
> here may enjoy...
>
> I do lots of HTML (actually, JSP) editing in my work, and so frequently
> have need to select the "innerHTML" of an pair of tags. I have installed
> matchit.vim(1) and set the following in my .vimrc:
>
> let b:match_words='<\([\:0-9A-Za-z_-]\+\)[^>]*>:<\/\1[^>]*>,<%:%>'
>
> That match_words string works nearly all of the time for HTML, XML, and
> so forth, but occasionally fails if the capitalization of the tags does
> not match -- it'll match forward, but not backwards.
>
> Then, I set the following mapping (I use a Mac, so I chose option-A
> because it is similar to command-A, which is "select all"):
>
> map å Vk%k
>
> I put the cursor on the first line of the "innerHTML" I want to select,
> then use the mapping, which goes up to the opening tag, uses % to find
> the appropriate closing tag, then goes up again to deselect the closing
> tag. Voilá -- I've selected the contents of a pair of tags.

I'm not sure if I have understood the interaction between b:match_words,
matchit.txt, and your mapping correctly but wouldn't

vit

be a much simplet approach to visually select the text between a tag and
its matching end tag? There's also

vat

to select a text and its surrounding tags.

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

Tony Mechelynck

unread,
Sep 2, 2007, 6:41:52 AM9/2/07
to vim...@googlegroups.com

Well, it would! Thanks for pointing out a text object I didn't know about.

Best regards,
Tony.
--
We gave you an atomic bomb, what do you want, mermaids?
-- I. I. Rabi to the Atomic Energy Commission

Dave Land

unread,
Sep 2, 2007, 11:41:32 AM9/2/07
to vim...@googlegroups.com
On Sep 2, 2007, at 3:13 AM, Jürgen Krämer wrote:

> I'm not sure if I have understood the interaction between
> b:match_words,
> matchit.txt, and your mapping correctly but wouldn't
>
> vit
>
> be a much simplet approach to visually select the text between a
> tag and
> its matching end tag? There's also
>
> vat
>
> to select a text and its surrounding tags.

It certainly does! I had no idea.

I'm glad to know about that text object!

Unfortunately, Vit and Vat don't work as I would have expected: upon
typing the final 't', the selection changes from line-oriented ('V')
to character-oriented (as though I'd typed 'v' to start).

But I will be using vit and vat to select text between tags when I
don't need full-line selections.

Dave

Tony Mechelynck

unread,
Sep 2, 2007, 12:34:54 PM9/2/07
to vim...@googlegroups.com

After an operator (but not v V Ctrl-V) and before the motion or object, you
can use v V or Ctrl-V to force te operator to act characterwise, blockwise or
linewise, respectively: e.g.:

dVit

delete inside the tag, and extend the delete to act on whole lines.


Best regards,
Tony.
--
The reader this message encounters not failing to understand is
cursed.

Jürgen Krämer

unread,
Sep 3, 2007, 1:45:01 AM9/3/07
to vim...@googlegroups.com

Hi,

Dave Land wrote:
>
> I'm glad to know about that text object!
>
> Unfortunately, Vit and Vat don't work as I would have expected: upon
> typing the final 't', the selection changes from line-oriented ('V')
> to character-oriented (as though I'd typed 'v' to start).

while in any visual selection mode you can use <V>, <v>, or <C-V> to
change to a different one. So just use

VitV

or

vitV

to select complete lines.

Reply all
Reply to author
Forward
0 new messages