views:

1619

answers:

4

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 ( " 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?

+28  A: 

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!

Greg Hewgill
Thanks, that was a useful answer. Do you see any reason not to implement % for quotes, though?
static_rtti
As you say, the difficulty with quotes is they're not naturally symmetric. Your idea about odd/even numbers seems like a good compromise, although it wouldn't be applicable to all situations. Care would have to be taken with escaped quotes (`\"`) or Python triple quotes (`"""`) or quotes in other contexts such as Perl regular expressions, or languages that support multi-line string literals. But maybe it's worth a try!
Greg Hewgill
+2  A: 

Greg has suggested a good way of doing this kind of thing, but for information, there is a discussion of the problem with matching quotes here.

Al
Not a very fruitful discussion... Thanks anyways!
static_rtti
+3  A: 

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.

Vereb
You can also combine these text movement commands with modification commands, so `cf"` changes up to and including the next quote, while `ct"` changes up to but *not* including the next quote.
Greg Hewgill
+3  A: 

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.

nelstrom