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

时间:2021-01-20 23:12:27

I have the following piece of code in a file that I opened in Vim:

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

<p>Hello *!</p>

How can I delete <p> and </p> tags but keep the contents between them? That is, what should I press to end with:

如何删除

标签,但保留它们之间的内容?也就是说,我要强调的是:

Hello *!

I know pressing d i t will do opposite.

我知道按d I t会做相反的事情。

I'm using Janus.

我用杰纳斯。

5 个解决方案

#1


70  

With the surround.vim plugin installed, press d s t to delete surrounding tag.

环绕。安装vim插件,按d t删除周围标签。

Similar shortcuts:

类似的快捷键:

  • d s ( - delete surrounding parentheses ()
  • d s(-删除圆括号()
  • d s " - delete surrounding double quotes ""
  • d ' s -删除周围的双引号"
  • d s ' - delete surrounding single quotes ''
  • d ' s -删除周围的单引号"

and so on...

等等……

#2


77  

A simple solution would be (with the cursor anywhere inside the tag):

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

yitvatp

What this does is:

什么这是:

  • y - yanks
  • y -美国佬
  • it - the inside of the tag
  • 标签的内部
  • vat - selects the entire tag
  • vat -选择整个标签。
  • p - pastes the previously yanked text over it
  • p -粘贴先前删除的文本

#3


6  

Love Randy's (+1) answer and I just learned about tag blocks! This is just a supplemental answer.

我喜欢兰迪(+1)的回答,我刚学过标签块!这只是一个补充的答案。

So yit means "yank inner tag block" and vat means "go to visual mode and select a (whole) tag block".

所以yit的意思是“yank inner tag block”和vat的意思是“进入视觉模式,选择一个(整个)标签块”。

This is just for those that are too lazy to read the help file:

这只是针对那些懒得阅读帮助文件的人:

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.

#4


3  

Map this to a key of your choice:

把它映射到你的选择的关键:

vat<Esc>da>`<da>

Taken from http://vim.wikia.com/wiki/Delete_a_pair_of_XML/HTML_tags

从http://vim.wikia.com/wiki/Delete_a_pair_of_XML/HTML_tags

#5


0  

I tried the dst solution with surround.vim on a larger html block. It works but it indents all child tags to the same level. It shouldn't change the indentation, messes everything up.

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

Randys solution with yitvatp works too, but leaves me with a blank line before and after the pasted tag.

Randys解决了yitvatp的工作,但是在粘贴标签之前和之后给我一个空白行。

Any perfect solution out there?

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

#1


70  

With the surround.vim plugin installed, press d s t to delete surrounding tag.

环绕。安装vim插件,按d t删除周围标签。

Similar shortcuts:

类似的快捷键:

  • d s ( - delete surrounding parentheses ()
  • d s(-删除圆括号()
  • d s " - delete surrounding double quotes ""
  • d ' s -删除周围的双引号"
  • d s ' - delete surrounding single quotes ''
  • d ' s -删除周围的单引号"

and so on...

等等……

#2


77  

A simple solution would be (with the cursor anywhere inside the tag):

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

yitvatp

What this does is:

什么这是:

  • y - yanks
  • y -美国佬
  • it - the inside of the tag
  • 标签的内部
  • vat - selects the entire tag
  • vat -选择整个标签。
  • p - pastes the previously yanked text over it
  • p -粘贴先前删除的文本

#3


6  

Love Randy's (+1) answer and I just learned about tag blocks! This is just a supplemental answer.

我喜欢兰迪(+1)的回答,我刚学过标签块!这只是一个补充的答案。

So yit means "yank inner tag block" and vat means "go to visual mode and select a (whole) tag block".

所以yit的意思是“yank inner tag block”和vat的意思是“进入视觉模式,选择一个(整个)标签块”。

This is just for those that are too lazy to read the help file:

这只是针对那些懒得阅读帮助文件的人:

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.

#4


3  

Map this to a key of your choice:

把它映射到你的选择的关键:

vat<Esc>da>`<da>

Taken from http://vim.wikia.com/wiki/Delete_a_pair_of_XML/HTML_tags

从http://vim.wikia.com/wiki/Delete_a_pair_of_XML/HTML_tags

#5


0  

I tried the dst solution with surround.vim on a larger html block. It works but it indents all child tags to the same level. It shouldn't change the indentation, messes everything up.

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

Randys solution with yitvatp works too, but leaves me with a blank line before and after the pasted tag.

Randys解决了yitvatp的工作,但是在粘贴标签之前和之后给我一个空白行。

Any perfect solution out there?

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