When having selected something in visual mode where I want to perform a find-and-replace I always have to run :'<,'>s/foo/bar/g
manually. Is there some way so that I can bind it to some key combination that I have to press whilst in visual mode, so that the command automatically appears with the cursor at s/[cursor here]
and when I press tab the cursor jumps to s/foo/[jump here]
?
1 Answer
For the first demand you can create a mapping in visual mode.
e.g.:
vnoremap s :s/
In visual mode if you hit s
it will make sure the command line is filled with: :'<,'>s/
For the second demand it is unclear how you want foo to be discovered or is it a constant foo
.
Remark: If you already searched for foo
in the previous search you could also have:
:'<,'>s//bar
as equivalent as:
:'<,'>s/foo/bar
-
I don't want it to be discovered, I want that the cursor jumps to the position where I would write the word I want to replace and then jumps to the position where I write the word I want to replace it with– RichardJul 11, 2022 at 20:46
-
With my proposition you would: 1. hit
s
, 2. type your word (e.g.foo
), 3. type/
(instead of<tab>
) and 4. type the replacing word (e.g.bar
). Would that fit the bill? Jul 11, 2022 at 20:49 -
<C-r><C-w>
and<C-r><C-a>
on the:
command-line, to insert the word (WORD) under the cursor.