Baptiste Wicht
Baptiste Wicht

Reputation: 7663

How to use the Vim global command on matches and on lines?

I was wondering if it was possible to run a command on the search match and not on the line.

For instance, with this file:

asdf - asdf
hjkl - hjkl

If I run :g/-/normal D it will delete the content of all lines, but I'd rather delete the content from the match to the end of the line.

Is that possible ? I don't need to have several matches per line only that the command starts at the correct position.

This is only an example, I could use substitute in that particular example, but this not what I'm looking for.

Upvotes: 1

Views: 150

Answers (3)

SergioAraujo
SergioAraujo

Reputation: 11790

How about this:

:%norm t-D

t- .......... until before -
D ........... delete till end of line

Upvotes: 1

Kent
Kent

Reputation: 195029

:%s/-.*//

are you looking for something like this?

Upvotes: 2

romainl
romainl

Reputation: 196466

You can't reuse the match in your command so you will have to move to that match explicitely:

:g/-/normal f-D

Upvotes: 1

Related Questions