Reputation: 6200
I see how to search and replace in specific lines, specifying by line number, and how to search and replace using the current line as reference to a number of lines down.
How do I search and replace in the current line only? I'm looking for a simple solution that does not involve specifying line numbers as the linked solutions do.
Upvotes: 86
Views: 62176
Reputation: 283
Confirm (c
) and Replace :
:s/foo/bar/gc
Find the occurrence of 'foo', and before replacing it with 'bar' ask for confirmation.
Vim gives you these options :
replace with bar (y/n/a/q/l/^E/^Y)?
y
- yesn
- noa
- replace all occurrencesq
- quitl
- replace the current and stop (last)^E
(CTRL + e) - scroll down^Y
(CTRL + y) - scroll upNow, in your case you can use the combination of y
, n
and l
to achieve your purpose.
Upvotes: 15
Reputation: 2127
Replace all occurrences of str1
with str2
in certain line:
:s/str1/str2/g
remove the g
option if you want to replace only the first occurrence.
Upvotes: 118
Reputation: 2272
If you want to search and replace all of the matched word in the current line, you could easily use simple substitute (s)
with g
modifier in command mode.
:s/search/replace/g
If you just want to search and replace the first matched word in the current line, just move away the g
modifier from your command.
:s/search/replace/
Ref: :help substitute
Upvotes: 20
Reputation: 91375
You can use .
for the current line, like:
:.s/old/new/
This will change old
by new
in the current line only.
Upvotes: 67