1

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
  • 1
    You might be interested in <C-r><C-w> and <C-r><C-a> on the : command-line, to insert the word (WORD) under the cursor.
    – D. Ben Knoble
    Commented Jul 12, 2022 at 14:31

1 Answer 1

2

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
3
  • 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
    – Richard
    Commented Jul 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? Commented Jul 11, 2022 at 20:49
  • yes, thanks a lot!
    – Richard
    Commented Jul 11, 2022 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.