'Delete forward from cursor to the end of HTML tag in VIM?

We can delete all contents in HTML tag with "dit".

But my question is, in the example, I want to keep the first line and delete the rest, without using a command like "7dd".

Is there a better way than use "7dd" in this case? Thank you.

<section className="booklist">
  <Book /> ==> I want to keep this line, and delete the rest
  <Book />
  <Book />
  <Book />
  <Book />
  <Book />
  <Book />
  <Book />
</section>


Solution 1:[1]

Here is a variant:

vit<Esc>d''
  • vit<Esc> visually selects the whole inner tag but what we really want is the side effect of moving the cursor to the end of the area so we leave visual mode.
  • d'' cuts from here to the line we were on just before the last vertical motion.

Here is another one:

V/sec/-<CR>d
  • V/sec/-<CR> enters visual-line mode and extend it to the next occurrence of sec minus one line.
  • d deletes the visually selected lines.

Solution 2:[2]

You could use d/<string> to remove everything up to the first occurrence of <string>.

To do this in your example: place a cursor at the third line on the first column and type: d/<\/sec and press <CR> (Enter) to remove lines.


The only drawback of this method is that it changes the last-search register ("/).

Solution 3:[3]

Another solution can be d/<string>/-

In this example: d/sec/-

However the cursor has to be at the beginning of first <Book />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 romainl
Solution 2
Solution 3 Kim Taeyeon