How to delete HTML tags, not the contents in Vim

HtmlVimKeyboard ShortcutsJanus

Html Problem Overview


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

<p>Hello stackoverflow!</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.

Html Solutions


Solution 1 - Html

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...

Solution 2 - Html

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

Solution 3 - Html

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.

Solution 4 - Html

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

Solution 5 - Html

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?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionretroView Question on Stackoverflow
Solution 1 - HtmlBenoitView Answer on Stackoverflow
Solution 2 - HtmlRandy MorrisView Answer on Stackoverflow
Solution 3 - HtmlRobView Answer on Stackoverflow
Solution 4 - HtmlFredrik PihlView Answer on Stackoverflow
Solution 5 - HtmljensbambauerView Answer on Stackoverflow