gts
gts

Reputation: 627

Vim undocumented side effect when using a numeric value instead of a flag when doing replacement?

While experimenting with Vim substitutions I noticed that something like s/\w*/(&)/2 behaves like 0,2s/\w*/(&). In other words having a numerical value where the g or i or c flags would be(or no flag at all), acts as the max limit of a range affecting the first match on every line.

E.g. running both versions of the above would produce the same output on the text below

alpha
beta
gamma

to

(alpha)
(beta)
gamma

However going through the Vim docs for s_flags as well as searching online I do not see this mentioned somewhere. Has anyone else come against this before and/or is there a perfectly normal explanation?

Upvotes: 1

Views: 57

Answers (1)

benjifisher
benjifisher

Reputation: 5122

From :help :s,

:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
            ...
            When [count] is given, replace in [count] lines,
            starting with the last line in [range].  When [range]
            is omitted start in the current line.
            ...

In other words, your 2 is a count, not a flag. It is not clear from this doc that the space is optional, but it seems to be (and I would not relish the job of rewriting it to make that clear).

Upvotes: 2

Related Questions