4

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.

ppcano
  • 2,831
  • 1
  • 24
  • 19
  • 4
    The help files (`:h v_p`) suggest `"0p`. – Nabb Oct 08 '11 at 15:16
  • 1
    Black hole register yessssss. For example http://stackoverflow.com/questions/54255/in-vim-is-there-a-way-to-delete-without-putting-text-in-the-register – Cascabel Oct 08 '11 at 23:02
  • No, black hole register almost solves the problem, but only **almost**. See my answer for the details. – dorserg Oct 09 '11 at 22:42

6 Answers6

8

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.

Randy Morris
  • 39,631
  • 8
  • 69
  • 76
2

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()
Community
  • 1
  • 1
dorserg
  • 5,454
  • 3
  • 24
  • 29
1

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
1

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.

ZyX
  • 52,536
  • 7
  • 114
  • 135
0

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).

JtotheR
  • 503
  • 5
  • 5
0

I use this mapping to replace the currently selected text with default register without yanking it:

vnoremap <leader>p "_dP
romainl
  • 186,200
  • 21
  • 280
  • 313