Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim delete starting from character to end of line

Tags:

vim

I'm trying to come up with something that will delete all text to end of line from a given character onwards.

E.g. in the example below I want to keep only the IP address:

192.168.2.121/32 -m comment --comment "blah blah bye bye"  -j DROP
10.1.3.207 -m comment --comment "much longer comment with all kinds of stuff" -j DROP
172.16.1.0/24 -m comment --comment "Drop this range" -j DROP

The pattern to remove from is -m, i.e., reading from left, the first "-" encountered. From that "-" to end-of-line should be deleted on every line in the file.

I'm stumped on this one, guidance would be appreciated.

like image 900
apprentice454 Avatar asked May 18 '16 08:05

apprentice454


People also ask

How do we cut to the end of the line vim?

Cutting (Deleting) dd - Delete (cut) the current line, including the newline character. 3dd - Delete (cut) three lines, starting from the line where the cursor is positioned, d$ - Delete (cut) everything from the cursor to the end of the line.

Should delete text from current position to the end of file?

Explanation: If the cursor is placed at the end of the file, then delete button is pressed to delete the text. To delete the file, we should use delete button. To delete the selected text, delete or backspace is used.

Which command would delete from the cursor to the end of the line in Vim?

The command dw will delete from the current cursor position to the beginning of the next word character. The command d$ (note, that's a dollar sign, not an 'S') will delete from the current cursor position to the end of the current line. D (uppercase D) is a synonym for d$ (lowercase D + dollar sign). Save this answer.


1 Answers

A global command would be a good fit

:g/-/norm nD

Explanation

:g         : Start a Global Command (:h :g for extra help on global commands)
/-         : Search for -
/norm nD   : Execute nD in Normal Mode where 
               n - jumps to the match
               D - delete to the end of the line
like image 70
Lieven Keersmaekers Avatar answered Nov 30 '22 05:11

Lieven Keersmaekers