Reputation: 56292
The % key is one of the best features of vim: it lets you jump from {
to }
, [
to ]
, and so on.
However, it does not work by default with quotes: Either "
or '
, probably because the opening and closing quote are the same character, making implementation more difficult.
Thinking a bit more about the problem, I'm convinced that it should be implemented, by counting if the number of preceding quotes is odd or even and jumping to the previous or next quote, accordingly.
Before I try to implement it myself, I'd just like to know if someone already has?
Upvotes: 58
Views: 18754
Reputation: 347
I know this is old - but if anyone comes here 6 years later looking for an answer, as long as you are on the same line as the quotation marks you can essentially do vi"c
to change whats within the quotation marks or va"c
to change everything including the quotation marks.
It will do the same as a potential %"
would do with the exception you have to be on the correct line, but that should be easily solved as you probably already know how to move around in vim.
Or if you're looking for single quotation marks use vi'c
instead.
Upvotes: 1
Reputation: 4358
I have found this technique very useful for going to the start/end of a very long quoted string.
vi"
or vi'
o
this actually takes the cursor next to the start/end quote character, but still feels pretty helpful.
Adding Stefan's excellent comment here which is a better option for anyone who may miss the comment.
If you use va" (and va') then it will actually visually select the quotes itself as well.
– Stefan van den Akker
Upvotes: 19
Reputation: 5498
I know this question is old but here is a plugin to use % to match the corresponding double quote:
https://github.com/airblade/vim-matchquote
Upvotes: 11
Reputation: 19552
I'd like to expand on Greg's answer, and introduce the surround.vim plugin.
Suppose that rather than editing the contents of your quotes, you want to modify the "
characters themselves. Lets say you want to change from double-quotes to single-quotes.
foo(bar, "baz quux")
^
The surround plugin allows you to change this to
foo(bar, 'baz quux')
^
just by executing the following: cs"'
(which reads: "change the surrounding double-quotes to single-quotes").
You could also delete the quote marks simply by running: ds"
(which reads: "delete the surrounding double-quotes).
There is a good introduction to the surround plugin here.
Upvotes: 10
Reputation: 14716
Greg's answer was very useful but i also like the 'f' and 'F' commands that move the cursor forward and backward to the character you press after the command.
So press f" to move to the next " character and F" to move to the previous one.
Upvotes: 48
Reputation: 993881
Depending on your reason for needing this, there may be a better way to accomplish what you're looking for. For example, if you have the following code:
foo(bar, "baz quux")
^
and your cursor happens to be at the ^
, and you want to replace everything inside the quotes with something else, use ci"
. This uses the Vim "text objects" to change (c
) everything inside (i
) the quotes ("
) and puts you in insert mode like this:
foo(bar, "")
^
Then you can start typing the replacement text. There are many other text objects that are really useful for this kind of shortcut. Learn (and use) one new Vim command per week, and you'll be an expert in no time!
Upvotes: 96