I am working on a simple text file in vim where I want to end every sentence with 2 spaces after full stop (dot/period). However, I do not want those sentences which already have 2 spaces after full stop to have further increase in spaces. The test text could be:
This sentence has only 1 space after it. This one has two. This line has again 1 space only. This is last line.
I tried:
%s/\. /\. /g
but this increases all spaces by one. I tried following also but it does not work:
%s/\. \\([^ ]\\)/. \\1/g
How can I achieve this in vim?
You may use
%s/\. \( \)\@!/. /g
The \. \( \)\@!
pattern matches a .
and a space that is not followed with another space.
The (...)@!
is a negative lookahead construct in Vim. See Lookbehind / Lookahead Regex in Vim. In other common regex flavors, it is written as (?!pattern)
. You may learn more about how negative lookaheads work in this answer of mine.
To match any whitespace, replace the literal space with \s
inside the pattern.