开发者

How to delete HTML tags, not the contents in Vim

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

<p>Hello s开发者_高级运维tackoverflow!</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 stackoverflow!

I know pressing d i t will do opposite.

I'm using Janus.


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

Similar shortcuts:

  • d s ( - delete surrounding parentheses ()
  • d s " - delete surrounding double quotes ""
  • d s ' - delete surrounding single quotes ''

and so on...


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

yitvatp

What this does is:

  • y - yanks
  • it - the inside of the tag
  • vat - selects the entire tag
  • p - pastes the previously yanked text over it


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

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

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.


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


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.

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

Any perfect solution out there?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜