New answer to cover the new requirements:
Here is a solution that should cover the new examples. This one even retains that pesky space before the comment in the third example that I struggled with in the original update.
%s/^.\{-}:\zs.\{-}\ze\%($\|;\|\s*--\)//
Explanation:
:%s/
- Start the substitution command for the entire file. If you want to operate only on the current line get rid of the %
.
^.\{-}:
- Starting from the beginning of the line, find as few characters as possible until the first :
.
\zs.\{-}\ze
- Find as few characters as possible. The \zs
and \ze
sets the start and end of the part that will be replaced (the some-thing
parts).
\%(
- Group the following characters until the \)
. Inside here we will specify all of the patterns, separated by \|
, that we don't want to replace at the end of the line.
$
- Match the end of the line. This covers the first example.
\|
- Basically a logical "OR". If we didn't match the previous pattern (the end of the line character) try matching the next pattern.
;
- Match a literal ;
. This covers the second and fourth examples.
\|
- Another logical "OR".
\s*--
- Match as many spaces as possible followed by --
. This covers the third example.
\)
- End the group started by \%(
.
//
- Defines the replacement pattern, which is nothing.
Original answer:
Providing your desired result for each example would be helpful to make sure the answers achieve what you're looking for.
If I'm understanding correctly, you want to replace all the text that comes after the first :
on the line and before a ;
. If there is no ;
, then replace the entire line after the :
.
If all that is correct, I would use:
:s/:\zs[^;]*//
The key part of this is the \zs
in the search pattern. This sets the start of the text that will be replaced. So, basically we look for a :
, then set the position where we will start replacing. Then, look for as many non-;
characters until the end of the line. Then replace everything from that start position to where ever we ended up.
If you want to perform the substitution on all of the lines at once, add a %
to the beginning:
:%s/:\zs[^;]*//
You can also use \ze
to mark the end of the replacement part. For example, if you only wanted to do the replacement only on lines with the ;
, you could do:
:s/:\zs[^;]*\ze;//
:help \zs
and :help \ze
explains this further.
If any of my assumptions were incorrect, please clarify the question and I can update my answer.
(Original) Update:
The following is as close as I'm able to get with a single expression. I can't seem to figure out a way to retain the space before the --
in your third example.
:s/:\zs[^;-]*//
The only difference from what I previously wrote is that we now also include -
as a character that we want to exclude from the match. This, of course, assumes that a -
won't exist in the something
part that you're trying to replace.
abc : some--thing
?--
is comment. Expected result isabc : --thing
.