Each time i copy a word and want to replace it for several words, i do:
After this process, the replaced word will be yanked and cannot continue replacing new words bceause i lost the first yanked word. So, i must copy again the first yanked word.
Could anybody guide to me on how to achieve my goal in an efficient way? It could be enough if my yanked word would not get changed.
I would suggest explicitly using a register for your yank and paste.
"ayw
or however you chose to yank your word."ap
to paste.In this case I've used the a
register but you could use whichever suits you.
It has been answered before: Vim: how to paste over without overwriting register.
Overall, crude vnoremap p "_dP
mapping will almost get you there, but it won't work well in a few edge cases (e.g. if a word you're replacing is at the end of the line).
The superior approach is to use this crazy-looking snippet (I wish I knew Vimscript at least half as good as the author of this):
" replace visual selection without overwriting default register
function! RestoreRegister()
let @" = s:restore_reg
return ''
endfunction
function! s:Repl()
let s:restore_reg = @"
return "p@=RestoreRegister()\<cr>"
endfunction
vnoremap <silent> <expr> p <sid>Repl()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With