Vim: Replace all the dots, but the last on the line

I am looking for a way to replace all points in a row except the last.

Example:

Blah - whatever - foo.bar.baz.avi should become Blah - whatever - foo bar bar.avi

Since I will have more than one line in the file, and the number of points depends on each line, I am looking for a universal solution, and not something like β€œReplace the first matches of X” when X is a constant.

+7
source share
3 answers

This seems like a trick:

 %s/\.\(.*\.\)\@=/ /g 

\@= is lookahead . It corresponds to a full stop if there are the following full stops.

See :help zero-width .

+11
source

As a variant of the first answer, you may prefer this syntax:

 %s/\.\ze.*\./ /g 

This allows you to claim that there will be the next full stop after the search statement.

+6
source

Another way that might be useful for learning (substitution inside visual selection) would be:
:g/^/normal $F.hv0:s/\%V\./ /g ^M
where ^M is entered with CTRL-V , Enter .

This means: for each line ( g/^/ ) enter $F. (go to the last point), visually select from the character to the left to the beginning of the line ( hv0 ), and then replace the points ( :s/\./ /g^M ) only inside the visual selection ( \%V ).

0
source

All Articles