如何删除HTML标签,而不是Vim中的内容

时间:2011-06-07 10:38:39

标签: html vim keyboard-shortcuts janus

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

<p>Hello stackoverflow!</p>

如何删除<p></p>标记但保留其中的内容?那就是,我应该按什么方式结束:

Hello stackoverflow!

我知道按 d i t 将会相反。

我正在使用Janus

5 个答案:

答案 0 :(得分:82)

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

yitvatp

这是做什么的:

  • y - yanks
  • it - 标记内部
  • vat - 选择整个标记
  • p - 粘贴之前已经上面的文字

答案 1 :(得分:76)

安装了surround.vim插件后,按 d s t d elete s urrounding t ag。

类似的快捷方式:

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

依旧......

答案 2 :(得分:6)

爱兰迪(+1)的答案,我刚刚学习了标签块!这只是一个补充答案。

因此,yit表示“抽出内部标记块”,而vat表示“进入可视模式并选择 a (整个)标记块”。

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

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.

答案 3 :(得分:4)

将此映射到您选择的键:

vat<Esc>da>`<da>

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

答案 4 :(得分:0)

我在更大的html块上尝试使用surround.vim的dst解决方案。它有效,但它将所有子标签缩进到同一级别。它不应该改变缩进,搞乱一切。

使用yitvatp的Randys解决方案也可以,但在粘贴的标记之前和之后留下了一个空行。

那里有完美的解决方案吗?