13

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.

7 Answers 7

27

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
4
  • Is there a global command to delete from the beginning of the line to a given character?
    – anishjp
    Feb 22, 2021 at 8:33
  • 2
    @anishjp - yes, try %norm 0dt<character> Feb 22, 2021 at 16:50
  • Thank you for that. If I have to also delete the given character?
    – anishjp
    Feb 24, 2021 at 9:27
  • 1
    @anishjp - %norm 0df<character> Feb 24, 2021 at 9:50
21

There's a simple method to do that under the normal mode:

  1. /-m to let the cursor move to the first occurrence "-m" in the file.
  2. Press d$ to delete characters from the cursor to the end of the line.
  3. Press n to find another "-m".
  4. Press . to redo step 2.
1
  • "Press d$ to delete characters from the cursor to the end of the line." this is what I was looking for, thank you.
    – Asmoox
    Aug 31, 2022 at 15:07
9

Isn't this as simple as:

:%s/-m.*//

?

Or I didn't understand the problem right?

3
  • 1
    In this case, %s/ .*// would do as well. May 18, 2016 at 9:26
  • @LucHermitte sure it is gonna work for OP's example data. however his requirement was from -m. So I would follow what OP said, instead of the example. Otherwise it could be as easy as :%norm! WD
    – Kent
    May 18, 2016 at 9:33
  • No, it's not simple. It's anything but simple.
    – kmonsoor
    Jun 4, 2018 at 15:31
2

I would do:

:%norm f D

"On each line, move the cursor to the first space and cut everything from the cursor to the end of the line."

:help range
:help :normal
:help f
:help D
1

I would register a macro, for example:

  1. Put the cursor on the first line, at position 0
  2. ql start registering a macro on the letter l
  3. t-D+
  4. q end the macro
  5. Launch the macro as many times as you want eg: 3@l to launch it three times

Explanation of t-D+:

  • t- goes in front of the next occurence of -
  • D delete till end
  • +, jumps to the next line at the beginning of the string so that we can chain macros (l should work too on vim as you deleted till the end)

As @Nobe4 stated you can also register the macro on one line (eg qlt-Dq) and then repeat on a visual selection: VG:normal!@l.

5
  • If you have a macro that operate line-wise, you can repeat it over a range of line with :'<,'>normal! @q, which may be useful also :)
    – nobe4
    May 18, 2016 at 8:19
  • '<,'> is the visual selection no? Btw it doesn't work to me.
    – soyuka
    May 18, 2016 at 8:21
  • strange, if you try your macro without the j0 at the end ? And to achieve this, you can select a set of line with v, then pressing : should give you :'<,'>
    – nobe4
    May 18, 2016 at 8:22
  • 1
    My bad, I used the wrong letter (you typed q I used l here ;)). Thanks for the tip, I'll improve my answer. Why doesn't this work with % as a buffer instead of the visual selection?
    – soyuka
    May 18, 2016 at 8:24
  • 1
    two comments: 1) d$ could be D 2) j0 could be +
    – Kent
    May 18, 2016 at 8:44
0

Select your text with visual mode and then use:

:'<,'>s/\([^- ]*\).*/\1/

Decomposition:

:'<,'>s/     " start a substitution on current selected lines
\([^- ]*\)   " capture a groupe of everything except a space and a -
.*/          " match the rest of the line
\1/          " replace by only the matched group
0
  1. Move the cursor to the beginning of the line need to be removed.
  2. press key d twice to remove the line.

ref: https://alvinalexander.com/linux/vi-vim-delete-line-commands-to-end/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.