In vim, is there a plugin to use % to match the corresponding double quote (")?
Asked Answered
M

6

57

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?

Misbehavior answered 18/9, 2009 at 11:46 Comment(0)
B
96

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!

Bannerol answered 18/9, 2009 at 11:56 Comment(3)
Thanks, that was a useful answer. Do you see any reason not to implement % for quotes, though?Misbehavior
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!Bannerol
I was going to comment "Great answer!", but with 612,000+ reputation, you probably already know that. ;)Ferullo
G
47

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.

Guilt answered 18/9, 2009 at 17:33 Comment(1)
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.Bannerol
I
19

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

Intervenient answered 4/2, 2018 at 7:5 Comment(2)
If you use va" (and va') then it will actually visually select the quotes itself as well.Counterstatement
go to start/end of the string by pressing o You sir are my hero!Whet
R
11

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

Rosenblast answered 25/1, 2017 at 15:5 Comment(0)
T
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.

Tandie answered 21/9, 2009 at 18:40 Comment(0)
E
1

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.

Eslinger answered 20/3 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.