0

Sometimes I find it convenient to

  1. Search for text I want to replace /original
  2. Start writing a substitution to replace that :s/
  3. Use C-r C-w to put the current word hovered into the command line :s/original

I think some vimrc settings can be changed to combine 1 and 2? I recently switched to neovim and haven't copied all of my plugins and vimrc across yet, but I seem to remember that typing :s/wo... would search in the text and show me partial matches, so I could immediately C-r C-w.

Anyway, after choosing the word to replace, I want to choose the replacement word which is a word that appears somewhere else in the file, without typing that whole word manually into the command line.

Unfortunately, while typing the replacement part in :s it seems like no words are highlighted in the file so I can't C-r C-w.

For example, in this text I want to replace original with replacement, without typing all of replacement.

lorem ipsum replacement dolor
original consectetur original
ullamcorper condimentum metus

How could I do it?

  • You don't have to type that word twice. The last search pattern is stored in @/, so you can do just /word_to_search and then s//replace_with – Matt May 10 at 10:13
  • @Matt I would do /wo<C-r><C-w> then I would do :s//re... what next? so I don't have to type all of replace_with? – theonlygusti May 10 at 11:14
  • If you copy the replacement text to some register (say 0) before starting the :s, you could do :s/original/\=getreg(0)/ – muru May 10 at 11:40
  • The last search pattern in in the register /. So you can insert it with <C-r>/. Only "better" if your last search was a regex not just a plain word. Regarding replacement: I don't think this is possible, but would be happy when someone proves me wrong. – Ralf May 10 at 11:52
  • I have some thoughts here—Not exactly a duplicate, but might help – D. Ben Knoble May 10 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.