Vim delete starting from character to end of line
Solution 1
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
Solution 2
There's a simple method to do that under the normal mode:
/-m
to let the cursor move to the first occurrence "-m" in the file.- Press
d$
to delete characters from the cursor to the end of the line. - Press
n
to find another "-m". - Press
.
to redo step 2.
Solution 3
Isn't this as simple as:
:%s/-m.*//
?
Or I didn't understand the problem right?
Solution 4
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
Solution 5
I would register a macro, for example:
- Put the cursor on the first line, at position
0
ql
start registering a macro on the letterl
t-D+
q
end the macro- 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
.
Admin
Updated on August 05, 2022Comments
-
Admin over 1 year
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.
-
nobe4 almost 8 yearsIf 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 :) -
soyuka almost 8 years'<,'> is the visual selection no? Btw it doesn't work to me.
-
nobe4 almost 8 yearsstrange, if you try your macro without the
j0
at the end ? And to achieve this, you can select a set of line withv
, then pressing:
should give you:'<,'>
-
soyuka almost 8 yearsMy bad, I used the wrong letter (you typed
q
I usedl
here ;)). Thanks for the tip, I'll improve my answer. Why doesn't this work with%
as a buffer instead of the visual selection? -
Kent almost 8 yearstwo comments: 1)
d$
could beD
2)j0
could be+
-
Luc Hermitte almost 8 yearsIn this case,
%s/ .*//
would do as well. -
Kent almost 8 years@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
-
kmonsoor almost 6 yearsNo, it's not simple. It's anything but simple.
-
anishjp about 3 yearsIs there a global command to delete from the beginning of the line to a given character?
-
Lieven Keersmaekers about 3 years@anishjp - yes, try
%norm 0dt<character>
-
anishjp about 3 yearsThank you for that. If I have to also delete the given character?
-
Lieven Keersmaekers about 3 years@anishjp -
%norm 0df<character>