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
> 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
--
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
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.
thanks to all of you for the elaborate answers.
good to see that vim has such a helpful community.
gabor
> 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
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
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)
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
> 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
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.
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.