Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

macvim: how to paste several times the same yanked word?

Tags:

vim

macvim

Each time i copy a word and want to replace it for several words, i do:

  1. yank the word
  2. enter visual mode, select the word to be replaced and paste the yanked word.

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.

like image 907
ppcano Avatar asked Dec 17 '22 08:12

ppcano


2 Answers

I would suggest explicitly using a register for your yank and paste.

  1. "ayw or however you chose to yank your word.
  2. "ap to paste.

In this case I've used the a register but you could use whichever suits you.

like image 92
Randy Morris Avatar answered Dec 18 '22 22:12

Randy Morris


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()
like image 37
dorserg Avatar answered Dec 18 '22 21:12

dorserg