Delete text in between HTML tags in vim?

HtmlVimEditor

Html Problem Overview


I know

di<

will delete in an HTML tag itself.

Is there an easy way to delete text in between two tags?

<span>How can I delete this text?</span>

Thanks!

Html Solutions


Solution 1 - Html

dit will delete the text between matching XML tags. (it is for "inner tag block".)

See :h it and :h tag-blocks.

Solution 2 - Html

cit
ci"

Two of the best productivity enabler commands of vim.

I save a lot of time and effort with just those two.

Solution 3 - Html

try dt< while the cursor is on the first character to delete. In your example the 'H'.

Solution 4 - Html

dit - delete inner tag, remains in command mode
cit - change inner tag, changes to edit mode

di" - delete inside ""
di' - delete inside ''

di( - delete inside ()
di) - delete inside ()

di[ - delete inside []
di] - delete inside []

di{ -delete inside {}
di} - delete inside {}

di< - delete inside <>
di> - etc

swap first letter d for c, if you want to be in edit mode after typing the command

Solution 5 - Html

(cursor on first character to delete) v/<[enter]d

This solution starts on the first character, then enters visual mode ("v"). It then searches for the next start bracket ("/<"), and then press enter to exit the search.

At this point, your visual selection will cover the text to delete. press d ("d") to delete it.

If I had to do this for a bunch of tags, I'd record the command and combine it with some other searches to make it repeatable. The key sequence might look like this:

[cursor on start of file] qa/>[enter]lv/<[enter]dnq

then press:

20@a

to do this for 20 tags

Solution 6 - Html

If you're aiming to do the inverse of deleting text between flags, I suggest installing Vim-Surround and running dst which deletes the surround tag

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
QuestionCMBView Question on Stackoverflow
Solution 1 - HtmlBrian CarperView Answer on Stackoverflow
Solution 2 - HtmlchipaironView Answer on Stackoverflow
Solution 3 - HtmlJD FriasView Answer on Stackoverflow
Solution 4 - HtmlKesView Answer on Stackoverflow
Solution 5 - HtmlbradView Answer on Stackoverflow
Solution 6 - HtmlNick SarafaView Answer on Stackoverflow