I can do :%s/
for replacing a string across a file, or :s/
:%s/search\|search2\|search3/replace/gci
g => global search
c => Ask for confirmation first
i => Case insensitive
:%s/search/replace/g
:%s/search/replace/gc
:%s/search/replace/gci
We don't need to bother entering the current line number.
If you would like to change each foo
to bar
for current line (.
) and the two next lines (+2
), simply do:
:.,+2s/foo/bar/g
If you want to confirm before changes are made, replace g
with gc
:
:.,+2s/foo/bar/gc
Replace All:
:%s/foo/bar/g
Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.
For specific lines:
:6,10s/foo/bar/g
Change each 'foo' to 'bar' for all lines from line 6 to line 10 inclusive.
To answer this question:
:40,50s/foo/bar/g
replace foo with bar in these lines between the 40th line and the 50th line(inclusive), when execute this command you can currently in any line.
:50,40s/foo/bar/g
also works, vim will ask you for comfirm and then do the replacement for you as if you have input the first command.
:,50s/foo/bar/g
replace foo with bar in these lines between the line you are currently in and the 50th line(inclusive). (if you are in a line AFTER the 50th line vim will ask for confirm again)
To clearity the difference between vim and the vrapper plugin of Eclipse:
Note that in varpper
:,50s/foo/bar/g
command will NOT work.
:50,40s/foo/bar/g
will work without asking for comfirmation.
(For Vrapper Version 0.74.0 or older).
In vim if you are confused which all lines will be affected, Use below
:%s/foo/bar/gc
Change each 'foo' to 'bar', but ask for confirmation first. Press 'y' for yes and 'n' for no. Dont forget to save after that
:wq
You can do it with two find/replace sequences
:6,10s/<search_string>/<replace_string>/g
:14,18s/<search_string>/<replace_string>/g
The second time all you need to adjust is the range so instead of typing it all out, I would recall the last command and edit just the range