54

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?

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
static_rtti
  • 53,760
  • 47
  • 136
  • 192

5 Answers5

94

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
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Thanks, that was a useful answer. Do you see any reason not to implement % for quotes, though? – static_rtti Sep 18 '09 at 11:58
  • 6
    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 Sep 18 '09 at 12:02
  • 1
    I was going to comment "Great answer!", but with 612,000+ reputation, you probably already know that. ;) – Joshua Pinter Apr 02 '18 at 07:21
44

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
  • 14,388
  • 2
  • 28
  • 30
  • 5
    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 Sep 18 '09 at 20:34
17

I have found this technique very useful for going to the start/end of a very long quoted string.

  1. when cursor is inside the string, visually select the whole string using vi" or vi'
  2. go to start/end of the string by pressing o
  3. press escape to exit visual select mode

this actually takes the cursor next to the start/end quote character, but still feels pretty helpful.

Edit

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

Community
  • 1
  • 1
th1rdey3
  • 4,176
  • 7
  • 30
  • 66
10

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.

Community
  • 1
  • 1
nelstrom
  • 18,802
  • 13
  • 54
  • 70
10

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

Andy Stewart
  • 5,013
  • 2
  • 28
  • 38