Delete text in between HTML tags in vim?

33,720

Solution 1

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

See :h it and :h tag-blocks.

Solution 2

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

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

Solution 4

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

(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

Share:
33,720

Related videos on Youtube

CMB
Author by

CMB

Updated on September 29, 2021

Comments

  • CMB
    CMB over 2 years

    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!

  • CMB
    CMB almost 15 years
    Also very useful for other situations, thanks! (thought I think it should be "dt<")
  • Debilski
    Debilski over 14 years
    And, of course, cit when you want to write immediately afterwards.
  • Kris Jenkins
    Kris Jenkins over 13 years
    And the Surround plugin (vim.org/scripts/script.php?script_id=1697) is awesome when you want to do things like change the surrounding tag (cst) from a <p> to a <div>, for example.
  • aiham
    aiham almost 12 years
    There is also dat (Delete A Tag block) which includes the actual tags. cit which is like dit but enters insert mode after. cat which is like dat but enters insert mode after. Also di" and di( for delete inner double quote and delete inner parenthesis respectively.
  • lyonsinbeta
    lyonsinbeta almost 12 years
    While this is technically true, it requires navigating to the first character you want to delete instead of just jumping anywhere into the line. But true is true. :-)
  • FelikZ
    FelikZ about 9 years
    @KrisJenkins can you show a full example how to achieve that? I can only do that with quotes/brackets.
  • Alex Moore-Niemi
    Alex Moore-Niemi about 9 years
    cit is covered in this thread, but for posterity, ci" will delete up to the next " found. great for changing class names in html tags, like <span id="really long annoying-id"> -- with cursor at first ", hit ci" and be dropped into inserting new characters between the quotes.
  • John Sparwasser
    John Sparwasser almost 8 years
    ciw is also useful as it deletes the current word; I use it more than cit and ci" actually.
  • pilat
    pilat over 7 years
    I used to use command like c/<[enter] a lot, in my "Vim life", but now I'm a bit regretful about that. You see, most of "Vi-mode" plugins for popular other editors/IDEs do not support this specific type of command. They support cit, ci", ct<and however. So, I'm re-teaching again.
  • Joe Freeman
    Joe Freeman over 7 years
    Also vat (or vit), followed by repeated at (or it) to progressively select surrounding tags . (Or v2at, etc). Then d to delete (etc).
  • schlimmchen
    schlimmchen almost 7 years
    @AlexMoore-Niemi: ci" will actually delete the text to the left until the next quote and to the right until the next quote and then enter insert mode. ct" is actually the command to "delete up to the next " found". But yes, it behaves the same if the cursor is under the left quote.
  • andho
    andho about 4 years
    @KrisJenkins You can use ci< to change within angled brackets.