170

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
  • 3,725
  • 5
  • 24
  • 19

5 Answers5

338

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

See :h it and :h tag-blocks.

Brian Carper
  • 66,618
  • 25
  • 158
  • 164
  • 31
    And, of course, `cit` when you want to write immediately afterwards. – Debilski Jan 29 '10 at 01:18
  • 6
    And the Surround plugin (http://www.vim.org/scripts/script.php?script_id=1697) is awesome when you want to do things like change the surrounding tag (cst) from a

    to a

    , for example.
    – Kris Jenkins Nov 21 '10 at 12:45
  • 4
    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. – aiham Jun 22 '12 at 00:05
  • @KrisJenkins can you show a full example how to achieve that? I can only do that with quotes/brackets. – FelikZ Mar 20 '15 at 09:01
  • 1
    Also `vat` (or `vit`), followed by repeated `at` (or `it`) to progressively select surrounding tags . (Or `v2at`, etc). Then `d` to delete (etc). – Joe Freeman Feb 16 '17 at 17:09
  • @KrisJenkins You can use `ci – andho Feb 19 '20 at 08:35
29
cit
ci"

Two of the best productivity enabler commands of vim.

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

chipairon
  • 1,841
  • 2
  • 19
  • 21
  • 3
    `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 `` -- with cursor at first `"`, hit `ci"` and be dropped into inserting new characters between the quotes. – Alex Moore-Niemi Apr 26 '15 at 16:49
  • 1
    `ciw` is also useful as it deletes the current word; I use it more than `cit` and `ci"` actually. – John Sparwasser Jun 30 '16 at 17:39
  • 3
    @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. – schlimmchen Aug 15 '17 at 14:36
9

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

JD Frias
  • 3,853
  • 3
  • 19
  • 21
  • Also very useful for other situations, thanks! (thought I think it should be "dt – CMB Jun 03 '09 at 18:21
  • 1
    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. :-) – lyonsinbeta Jul 19 '12 at 18:04
4

(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

brad
  • 2,103
  • 3
  • 23
  • 26
-1

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

Nick Sarafa
  • 938
  • 10
  • 16