I have a block of of text which contains variables whose name I want to change, but not all. I want to be able to choose which variables get replaced by the substitution and which do not. I tried using :%s/pattern/string/c
, but the problem with that is that it starts from the beginning of the file, which also has the variables which I don't want to replace.
From :h :s
:
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
For each line in [range] replace a match of {pattern}
with {string}.
For the {pattern} see |pattern|.
{string} can be a literal string, or something
special; see |sub-replace-special|.
When [range] and [count] are omitted, replace in the
current line only. When [count] is given, replace in
[count] lines, starting with the last line in [range].
When [range] is omitted start in the current line.
Specifically:
When [count] is given, replace in count] lines, starting with the last line in [range].
When [range] is omitted start in the current line.
My plan was to run the substitution once, then repeat it using &
as many times as needed.
However, if I do :s/pattern/string 1
, unless the pattern exists in the current line, the substitution cannot find the pattern. I can only conclude I misunderstood the wording.
What is the best way to achieve what I want?
c
flag with a range doesn't work? E.g.20,100s/pattern/string/c
.... Also, do you have'gdefault'
set?gdefault
set.'gdefault'
you should be usingg
flag, no? I'm not quite sure what you mean by "I would want the process of defining the range". The process?g
flag controls if there should be only one substitution per line or more. In any case, both options don't work as I intend.g
does. I guess you're implying that you don't need to replace more than one word per line.