71

我在打开的文件中有以下代码Vim

<p>Hello stackoverflow!</p>

如何删除<p></p>标记但保留它们之间的内容?也就是说,我应该按什么结束:

Hello stackoverflow!

我知道按下d i t会适得其反。

我正在使用Janus

4

5 回答 5

100

安装了环绕.vim插件后,d s t按删除环绕标签

类似的快捷键:

  • d s (- 删除周围的括号()
  • d s "- 删除周围的双引号""
  • d s '- 删除周围的单引号''

等等...

于 2011-06-07T11:09:13.680 回答
94

一个简单的解决方案是(将光标放在标签内的任何位置):

yitvatp

这是做什么的:

  • y- 猛拉
  • it- 标签的内部
  • vat- 选择整个标签
  • p- 将先前拉出的文本粘贴到它上面
于 2011-06-07T11:05:12.877 回答
6

喜欢 Randy (+1) 的回答,我刚刚了解了标签块!这只是一个补充答案。

所以 yit 的意思是“拉动内部标签块”,vat 的意思是“进入可视模式并选择一个(整个)标签块”。

这仅适用于那些懒得阅读帮助文件的人:

Tag blocks                      *tag-blocks*

For the "it" and "at" text objects an attempt is done to select blocks between
matching tags for HTML and XML.  But since these are not completely compatible
there are a few restrictions.

The normal method is to select a <tag> until the matching </tag>.  For "at"
the tags are included, for "it" they are excluded.  But when "it" is repeated
the tags will be included (otherwise nothing would change).  Also, "it" used
on a tag block with no contents will select the leading tag.

"<aaa/>" items are skipped.  Case is ignored, also for XML where case does
matter.

In HTML it is possible to have a tag like <br> or <meta ...> without a
matching end tag.  These are ignored.

The text objects are tolerant about mistakes.  Stray end tags are ignored.
于 2011-06-07T15:13:57.070 回答
4

将其映射到您选择的键:

vat<Esc>da>`<da>

取自http://vim.wikia.com/wiki/Delete_a_pair_of_XML/HTML_tags

于 2011-06-07T10:43:31.060 回答
0

我在一个更大的 html 块上尝试了带有环绕.vim 的 dst 解决方案。它可以工作,但它将所有子标签缩进到同一级别。它不应该改变缩进,把一切都搞砸了。

带有 yitvatp 的 Randys 解决方案也可以,但在粘贴标签之前和之后给我留下了一个空白行。

有什么完美的解决方案吗?

于 2018-03-29T08:47:51.417 回答