vim中,有没有插件可以使用%来匹配对应的双引号(")?

发布于 2024-08-05 07:21:20 字数 305 浏览 1 评论 0 原文

% 键是 vim 最好的功能之一:它可以让你从 { 跳转到 }[],等等。

但是,默认情况下,它不适用于引号: "',可能是因为开始和结束引号是相同的字符,使得实现更加困难。

思考一下关于这个问题的更多信息,我确信应该通过计算前面的引号数量是奇数还是偶数并相应地跳转到上一个或下一个引号来实现它,

在我尝试自己实现它之前,我只需这样做。想知道是否有人已经拥有?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鸠魁 2024-08-12 07:21:20

根据您需要此功能的原因,可能有更好的方法来完成您正在寻找的任务。例如,如果您有以下代码:

foo(bar, "baz quux")
              ^

并且您的光标恰好位于 ^ 处,并且您想用其他内容替换引号内的所有内容,请使用 ci"这使用 Vim "文本对象" 来更改 ( c) (i) 引号 (") 内的所有内容,并将您置于插入模式,如下所示:

foo(bar, "")
          ^

然后您可以开始输入替换文本。还有许多其他文本对象对于这种快捷方式确实有用。每周学习(并使用)一个新的 Vim 命令,您很快就会成为专家!

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!

无人问我粥可暖 2024-08-12 07:21:20

格雷格的回答非常有用,但我也喜欢“f”和“F”命令,它们将光标向前和向后移动到命令后按下的字符。

因此,按 f" 移动到下一个 " 字符,按 F" 移动到上一个字符。

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.

守不住的情 2024-08-12 07:21:20

我发现这种技术对于转到很长的带引号字符串的开头/结尾非常有用。

  1. 当光标位于字符串内部时,使用 vi"vi' 直观地选择整个字符串,
  2. o 转到字符串的开头/结尾
  3. 键退出视觉选择模式

,这实际上会将光标置于开始/结束引号字符旁边,但仍然感觉非常有用。

转义

如果您使用 va" (和 va'),那么它实际上也会在视觉上选择引号本身。

——斯特凡·范登阿克

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

树深时见影 2024-08-12 07:21:20

我知道这个问题很旧,但这里有一个插件使用 % 来匹配相应的双引号:

https:// github.com/airblade/vim-matchquote

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

锦欢 2024-08-12 07:21:20

我想扩展 Greg 的回答,并介绍 环绕.vim 插件。

假设您不想编辑引号的内容,而是想要修改 " 字符本身。假设您想要从双引号更改为单引号。

foo(bar, "baz quux")
              ^

环绕插件允许您更改此设置 只需执行以下命令即可

foo(bar, 'baz quux')
              ^

cs"'(内容为:“将周围的双引号更改为单引号”)。

您还可以通过运行以下命令来删除引号:ds"(其内容为:“删除周围的双引号)。

对环绕插件有很好的介绍这里

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.

蓝眼睛不忧郁 2024-08-12 07:21:20

我知道这已经很旧了 - 但如果有人在 6 年后来这里寻找答案,只要你与引号在同一行,你基本上可以执行 vi"c 来更改其中的内容引号或 va"c 来更改包括引号在内的所有内容。

它的作用与潜在的 %" 的作用相同,只是你必须位于正确的行上,但这应该很容易解决,因为你可能已经知道如何在 vim 中移动。

或者如果您要查找单引号,请使用 vi'c 代替。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文