macvim: how to paste several times the same yanked word?
Each time i copy a word and want to replace it for several words, i do:
- yank the word
- 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开发者_如何学Go 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()
Personally, I'd favour doing :s/word/replacement words/gc
.
Alternatively, you could use "_de
to delete the word to be replaced. "_
says use the "black hole" buffer to prevent losing the existing default buffer contents.
Perhaps a bit better than this is to yank the replacement words into an alternative named buffer (e.g. "a3ye
), then you can delete the work to be replaced (de
) and paste the named buffer "ap
.
One addition to @Randy Morris answer: instead of specifying register explicitly in both cases, you can specify it only in the second one, see :h quote0
(«Numbered register 0 contains the text from the most recent yank command...»). In this case using a
register is better (as it is much easier to type), but if you say you are replacing words, you may want to use ciw<C-r>0
and then one .
for each other word you want to replace.
I use this mapping to replace the currently selected text with default register without yanking it:
vnoremap <leader>p "_dP
I dont use yank, but ciw
and then repeat with .
For instance:
Go to somewhere inside the word you want to replace.
Do ciw <type new word> Esc
Go to somewhere inside the next word you want to replace.
Press .
to repeat the last replace.
Advanced:
You can also first find the word with /<word>
and then use ciw <new word>
.
Then you dont have to move to the word yourself before pressing .
but you can just use n
to go to the next (or N
to go to the previous).
精彩评论