如何在 Vim 中的括号(或引号或...)之间进行选择?
我确信曾经有一个用于此类东西的插件,但现在我需要它,我似乎无法找到它(自然),所以我只会问一个简单的问题。
在括号、引号或匹配字符列表之间进行选择的最简单方法是什么?
write ( *, '(a)' ) 'Computed solution coefficients:'
例如,在这里我想选择(a)
,或计算解系数:
。
我对多行不感兴趣,只对发生在一行上的情况感兴趣。
I'm sure there used to be a plugin for this kinda stuff, but now that I need it, I can't seem to find it (naturally), so I'll just ask nice and simple.
What is the easiest way to select between brackets, or quotes, or generally a list of matching characters?
write ( *, '(a)' ) 'Computed solution coefficients:'
For example, here I'd like to select (a)
, or Computed solution coefficients:
.
I'm not interested in multiline, just cases which occur on one line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
要在单引号之间进行选择,我通常执行
vi'
(“选择内部单引号”)。在括号块内,我使用
vib
(“选择内部块”)在花括号块内,您可以使用
viB
(“大写 B”)来使选择“包含” "(同时选择引号、圆括号或大括号)您可以使用
a
而不是i
。您可以阅读手册上有关文本对象选择的更多信息,或者< vim 中的 code>:help text-objects。
To select between the single quotes I usually do a
vi'
("select inner single quotes").Inside a parenthesis block, I use
vib
("select inner block")Inside a curly braces block you can use
viB
("capital B")To make the selections "inclusive" (select also the quotes, parenthesis or braces) you can use
a
instead ofi
.You can read more about the Text object selections on the manual, or
:help text-objects
within vim.使用您想要进入括号内的任何导航键,然后您可以使用
yi(
或yi)
复制匹配括号内的所有内容。 这也适用于方括号(例如yi]
)和花括号。 除了y
之外,您还可以删除或更改文本(例如ci)
、di]
)。我用双引号和单引号尝试过,它似乎也适用。 对于您的数据,我这样做:
将光标移至
C
,然后输入yi'
。 将光标移动到空行,点击p
,然后获取As CMS 已注明,这也适用于视觉模式选择 - 只需使用
vi)
、vi}
、vi'
等。Use whatever navigation key you want to get inside the parentheses, then you can use either
yi(
oryi)
to copy everything within the matching parens. This also works with square brackets (e.g.yi]
) and curly braces. In addition toy
, you can also delete or change text (e.g.ci)
,di]
).I tried this with double and single-quotes and it appears to work there as well. For your data, I do:
Move cursor to the
C
, then typeyi'
. Move the cursor to a blank line, hitp
, and getAs CMS noted, this works for visual mode selection as well - just use
vi)
,vi}
,vi'
, etc.这种选择方法是内置的,并在 Vim 帮助中详细介绍。 它涵盖了 XML 标签等。
请参阅
:帮助文本对象
。This method of selection is built-in and well covered in the Vim help. It covers XML tags and more.
See
:help text-objects
.要在单引号内进行选择,请使用
vi'
。要在括号内进行选择,请使用
vi(
.For selecting within single quotes use
vi'
.For selecting within parenthesis use
vi(
.使用箭头或
hjkl
到达括号表达式之一,然后使用v
选择视觉(即选择)模式,然后使用%
跳转到另一个支架。Use arrows or
hjkl
to get to one of the bracketing expressions, thenv
to select visual (i.e. selecting) mode, then%
to jump to the other bracket.停在括号开头,然后全选并打印:
如果要删除,请将
vi
替换为di
Stop at the beginning of brackets, and then select all and print:
if you want to delete replace
vi
withdi
我想对已经很好的答案进行补充。 我来这里寻找一种方法来更改 html 括号内的文本,所以我想为其他也在寻找该方法的人提供答案。
您可能认为
ci<
会起作用,但实际上只有当您位于其中一个标签本身时才有效:我想要的是更改 html 标签本身之间的文本:
我想要的是“更改inside tag":
cit
感谢您提到文档(
:help text-objects
)的其他答案,这就是我找到我正在寻找的内容的方式。I wanted to add to the already good answers. I came here looking for a way to change the text inside of html brackets, so I want to provide an answer for anyone else that is also looking for that.
You might think
ci<
would work, but actually that only works if you are inside one of the tags themselves:What I wanted was to change the text between the html tags themselves:
What I wanted was "change inner tag":
cit
Thank you to the other answer that mentioned the documentation (
:help text-objects
) which is how I found what I was looking for.使用
searchpair
内置函数在 .vimrc 中编写 Vim 函数:(http://vimdoc.sourceforge.net/htmldoc/eval.html)
Write a Vim function in .vimrc using the
searchpair
built-in function:(http://vimdoc.sourceforge.net/htmldoc/eval.html)
我会在投票最多的答案中添加一个细节:
如果您使用 gvim 并想要复制到剪贴板,请使用
"+
复制括号(或括号或大括号)
例如:
"+yi}
会将光标所在大括号之间的所有内容复制到剪贴板。I would add a detail to the most voted answer:
If you're using gvim and want to copy to the clipboard, use
"+<command>
To copy all the content between brackets (or parens or curly brackets)
For example:
"+yi}
will copy to the clipboard all the content between the curly brackets your cursor is.我制作了一个插件
vim-textobj-quotes
: https:// github.com/beloglazov/vim-textobj-quotes它为任何类型的最接近的引号对提供文本对象。 仅使用
iq
或aq
即可对当前光标周围的单引号 (')、双引号 (") 或反引号 (`) 的内容进行操作,位于光标前面或后面(按照优先顺序),当需要到达引号时,它会向前或向后跳转。通过查看示例更容易理解(光标用
显示)。 |
):foo '1, |2, 3' bar
;按diq
后:foo '|' bar
foo| '1, 2, 3' bar
;按diq
后:foo '|' bar
foo '1, 2, 3' |bar
;按diq
后:foo '|' bar
foo '1, |2, 3' bar
; 按daq
后:foo | bar
foo| '1, 2, 3' bar
; 按daq
后:foo | bar
foo '1, 2, 3' |bar
; 按daq
后:foo | bar
上面的示例是针对单引号,该插件的工作方式与双引号 (") 和反 (`) 引号。
您还可以使用任何其他运算符:
ciq
、diq
、yiq
、viq
等。请查看在上面链接的 github 页面上了解更多详细信息。
I've made a plugin
vim-textobj-quotes
: https://github.com/beloglazov/vim-textobj-quotesIt provides text objects for the closest pairs of quotes of any type. Using only
iq
oraq
it allows you to operate on the content of single ('), double ("), or back (`) quotes that currently surround the cursor, are in front of the cursor, or behind (in that order of preference). In other words, it jumps forward or backwards when needed to reach the quotes.It's easier to understand by looking at examples (the cursor is shown with
|
):foo '1, |2, 3' bar
; after pressingdiq
:foo '|' bar
foo| '1, 2, 3' bar
; after pressingdiq
:foo '|' bar
foo '1, 2, 3' |bar
; after pressingdiq
:foo '|' bar
foo '1, |2, 3' bar
; after pressingdaq
:foo | bar
foo| '1, 2, 3' bar
; after pressingdaq
:foo | bar
foo '1, 2, 3' |bar
; after pressingdaq
:foo | bar
The examples above are given for single quotes, the plugin works exactly the same way for double (") and back (`) quotes.
You can also use any other operators:
ciq
,diq
,yiq
,viq
, etc.Please have a look at the github page linked above for more details.
vim 中的一个简单的键盘映射可以解决这个问题。
地图 viq F”lvf”hh
上面的命令将 viq 映射到键以在引号之间进行搜索。 将 " 替换为任何字符并创建您的键盘映射。
在启动时将其粘贴到 vimrc 中,您应该每次都可以使用它。
A simple keymap in vim would solve this issue.
map viq F”lvf”hh
This above command maps viq to the keys to search between quotes. Replace " with any character and create your keymaps.
Stick this in vimrc during startup and you should be able to use it everytime.