Kim Taeyeon
Kim Taeyeon

Reputation: 13

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>

Upvotes: 0

Views: 281

Answers (4)

Kim Taeyeon
Kim Taeyeon

Reputation: 13

Another solution can be d/<string>/-

In this example: d/sec/-

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

Upvotes: 0

romainl
romainl

Reputation: 196886

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.

Upvotes: 1

OhleC
OhleC

Reputation: 2950

You can't directly move to the beginning or end of text objects, I think, but one way to achieve what you want in your example and keep the advantage of using the "tag" text object would be

Vitojd

As in, line-wise visually select the contents of the tag, jump to the first line of the selection, de-select it (by moving down), delete.

Upvotes: 1

jubnzv
jubnzv

Reputation: 1584

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 ("/).

Upvotes: 0

Related Questions