跳转到 Vim 中匹配的 XML 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/500989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jump to matching XML tags in Vim
提问by Nathan Fellman
Vim %operator jumps to matching parentheses, comment ends and a few other things. It doesn't, however, match XML tags (or any other tag, to the best of my knowledge).
Vim%操作符跳转到匹配的括号、注释结束和其他一些事情。但是,它不匹配 XML 标签(或任何其他标签,据我所知)。
What's the best way to jump to the matching XML tag using Vim?
使用 Vim 跳转到匹配的 XML 标签的最佳方法是什么?
Note:What I reallywant to do is duplicate a section in an XML file without manually looking for the matching tag.
注意:我真正想做的是在 XML 文件中复制一个部分,而无需手动查找匹配的标签。
采纳答案by sykora
There is a vim plugin called matchit.vim . You can find it here: http://www.vim.org/scripts/script.php?script_id=39. It was created pretty much the exact purpose you describe.
有一个名为 matchit.vim 的 vim 插件。你可以在这里找到它:http: //www.vim.org/scripts/script.php?script_id=39。它的创建几乎完全符合您所描述的目的。
Install that, place your cursor on the body of the tag (not the <>, else it'll match those) and press % to jump to the other tag. See the script's page to find out what else it matches.
安装它,将光标放在标签的主体上(不是 <>,否则它会匹配那些),然后按 % 跳转到另一个标签。查看脚本的页面以找出它匹配的其他内容。
回答by Robert Lujo
You can do this without additional plugins:
您可以在没有其他插件的情况下执行此操作:
- place cursor on the tag
- vat- will select the (outer) tag and place cursor on the end
- once you've got your selection you can toggle between the top and bottom with o(update based on Michael Gruber's note)
- c- change or, y- copy or, escape for leaving visual mode ...
- 将光标放在标签上
- vat- 将选择(外部)标签并将光标放在末尾
- 一旦你有了你的选择,你可以在顶部和底部之间切换o(更新基于迈克尔格鲁伯的笔记)
- c- 更改或,y- 复制或,退出视觉模式...
Another useful operation is: vit- will select content of the tag (inner).
另一个有用的操作是:vit- 将选择标签的内容(内部)。
Update (thanks to @elrado)Example: vitowill enable you to select inner content of the tag and position cursor on the beginning of the selected text.
更新(感谢@elrado)示例:vito将使您能够选择标签的内部内容并将光标定位在所选文本的开头。
Reference: https://superuser.com/questions/182355/how-can-i-select-an-html-tags-content-in-vim
参考:https: //superuser.com/questions/182355/how-can-i-select-an-html-tags-content-in-vim
Vim reference (thanks to @Geek for noting this out):
Vim 参考(感谢@Geek 指出这一点):
:help visual-operators
you'll get:
你会得到:
4. Operating on the Visual area *visual-operators*
The objects that can be used are:
...
at a <tag> </tag> block (with tags) |v_at|
it inner <tag> </tag> block |v_it|
...
回答by Codie CodeMonkey
The OP stated that what he really wanted to do is copy a section of XML without having to find the matching tag. This is easily done in normal mode with yat<motion>p, which yanks the text inside and including the matching tags, then pastes it. yit<motion>pis almost the same, but it doesn't include the outer tags.
OP 表示他真正想做的是复制一段 XML,而不必找到匹配的标签。这在普通模式下很容易用 完成yat<motion>p,它会拉出里面的文本并包括匹配的标签,然后粘贴它。 yit<motion>p几乎相同,但不包括外部标签。
The 'y' in the string is of course the normal mode "yank" command. (:help y)
字符串中的“y”当然是正常模式的“yank”命令。( :help y)
aor ican be used for object selection after an operator such as yor inside a visual selection. The symbol after aor ispecifies what should be selected. The object type tused here indicates an SGML tag. (:help object-select).
a或i可用于在操作符之后选择对象,例如y视觉选择或在视觉选择内。a或后面的符号i指定应选择的内容。t这里使用的对象类型表示一个 SGML 标签。( :help object-select).
Of course <motion>just means to move somewhere by the means of your choice and pputs the yanked text at that location.
当然<motion>只是意味着通过您选择的方式移动到某个地方,p并将被拉出的文本放在那个位置。
回答by transang
Just my trick of using "yank", "object-select" (tag select) and "jump to last yanked text".
只是我使用“猛拉”、“对象选择”(标签选择)和“跳到最后一个猛拉文本”的技巧。
yit`]
to jump to right before closing tag
在关闭标签之前向右跳
and
和
yit
to jump to right after opening tag
打开标签后向右跳转
Note: this will change the content of default register
注意:这会改变默认寄存器的内容

