Delete text in between HTML tags in vim?
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
Related videos on Youtube
CMB
Updated on September 29, 2021Comments
-
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 almost 15 yearsAlso very useful for other situations, thanks! (thought I think it should be "dt<")
-
Debilski over 14 yearsAnd, of course,
cit
when you want to write immediately afterwards. -
Kris Jenkins over 13 yearsAnd 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 almost 12 yearsThere 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 almost 12 yearsWhile 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 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 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"
, hitci"
and be dropped into inserting new characters between the quotes. -
John Sparwasser almost 8 years
ciw
is also useful as it deletes the current word; I use it more thancit
andci"
actually. -
pilat over 7 yearsI 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 supportcit
,ci"
,ct<
and however. So, I'm re-teaching again. -
Joe Freeman over 7 yearsAlso
vat
(orvit
), followed by repeatedat
(orit
) to progressively select surrounding tags . (Orv2at
, etc). Thend
to delete (etc). -
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 about 4 years@KrisJenkins You can use
ci<
to change within angled brackets.