2011-06-07 156 views
58

,我有以下的,我在Vim打开的文件的代码:如何删除HTML标签,而不是内容的Vim

<p>Hello stackoverflow!</p> 

如何删除<p></p>标签,但保留的内容他们?也就是说,我应该怎么按,以结束与:

Hello stackoverflow! 

我知道按d我吨会做相反。

我正在使用Janus

回答

69

随着surround.vim插件安装,按dst to d elete s urrounding t ag。

类似的快捷方式:

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

等等...

+0

这是我的解决方案。谢谢。 – retro 2011-06-07 11:13:43

+1

这应该是最有价值的答案,因为每个人都应该使用TPopes真棒插件这些天! :) – walialu 2014-11-14 07:50:05

+0

我知道'di ...'和'da ...',但我刚刚了解到'ds ...'。非常有用! – 2017-06-20 16:51:01

3

地图这对您选择的关键:从http://vim.wikia.com/wiki/Delete_a_pair_of_XML/HTML_tags

+0

谢谢你,我看到了这一点。但我想像dit一样映射它。例如点。 – retro 2011-06-07 11:09:05

+1

@retro将它映射到“dot”通常不是一个好主意,因为这会影响其他非常方便的vim命令,即“单重复” – 2011-06-07 11:14:11

+0

是的,我现在明白了。 – retro 2011-06-07 11:15:44

76

两者

vat<Esc>da>`<da> 

一个简单的解决方法是(用光标在标签内的任何地方):

yitvatp 

什么这是的:

  • y - 猛拉
  • it - 标签
  • vat的内侧 - 选择整个标签
  • p - 糊剂在它前面抽出的文本
+4

非常优雅的方式来做到这一点。 – Benoit 2011-06-07 11:09:32

+0

真的很好兰迪! 我使用的是surround.vim插件,所以Benoit的答案对我来说是真正的解决方案。 – retro 2011-06-07 11:14:45

+1

非常好。我发布了一个补充答案,以提供更多细节。感谢兰迪! – Rob 2011-06-07 15:14:30

6

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

所以yit的意思是“内藏标签块”,而桶意味着“进入视觉模式并选择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.