Reputation: 71
I am trying to clean up some code and I am trying to find a good way of achieving the following:
I am a #decent
guy
and I want:
I am a guy
I tried using
:g/#/d
but the whole line gets deleted and I only want to delete until the end of line. What is the best way to achieve this in vim?
Thank you.
Upvotes: 2
Views: 3491
Reputation: 2789
press d
+ shift 4
or d
+ $
, which means delete to end of the line
d
means deleteshift 4
or $
means cursor to end of the lineUpvotes: 0
Reputation: 564
Try this instead:
:s/ # .*\n/ /
Explanation:
You were using the wrong command, as they may look similar to new users.
:[range]g/VRE/act
Globally apply the "act"ion (one letter command) to all lines (in range, default all file) matching the VRE (pattern):[range]s/VRE/repl/f
Substitute within lines (in range, default current line) the matching VRE (pattern) with the "repl"acement using optional "f"lagsNow about the pattern, I think this candidate cover most cases (all but comments at the beginning of a line and comments without space after pound sign)
#
litteral space, then hash tag, then space again.*
dot for any character, star to mean the previous may occur many times or even be absent$
dollar at end to stay at "end of line", but \n
to catch en EOL hereUpvotes: 0
Reputation: 4673
With :global
you would want something like
:global/#/normal! f#D | join
or
:global/#/substitute/#.*// | join
Upvotes: 1
Reputation: 26074
That won't because the usage of that command:
:[range]g/pattern/cmd
defaults to range being the whole line, and you are not doing any substitution anyway.
Use:
:%s/#.\+\n//g
instead.
#
Matches a literal #..\+\n
Matches everything until the end of line, and a new line.//
Replaces the entire match with nothing.Upvotes: 4