Is there any option to replace the first occurrence without asking for confirmation in vim?
I want to replace New York with \place{New York}
in the first occurrence only without asking for confirmation. I used this code:
silent! /\\begin{text}/,/\\end{textit}/s/New York/\\place{New York}/g
> \begin{text}..[paragraphs]...\end{textit}
New York
may be contained 10 times in the paragraph.
So you want to in a line range do only single substitution, not one sub per line. So the /start/,/end/ s/pat/rep/
will not work. I would make your example simpler, given that we have:
foo
nyc nyc
nyc nyc
bar
I guess what you are expecting to have is
foo
\\place{nyc} nyc
nyc nyc
bar
However :/foo/,/bar/ s/nyc/\\place{&}
will give
foo
\\place{nyc} nyc
\\place{nyc} nyc
bar
Because :s
will do sub for each line in range.
I would narrow the range to solve this problem:
/foo//nyc/s/nyc/\\place{&}/
note that between /foo/ and /nyc/
, there is no comma.
The output should be:
foo
\\place{nyc} nyc
nyc nyc
bar
You may want to read :h range
for details.