Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Their is a kind of typing I do a lot :

When I'm everywhere on a word I like to copy it, so i have a simple

:nmap <Leader>y yiw

And then I'm going to a another word (everywhere on it) to replace with te yanked one, so I write

:nnoremap cp "_diwP

(cp for clear and paste)

example :

previous yanked word : "hello"

It work perfectly if I do cp on "bar" (it replace with the previous yanked)

lorem ipsum bar foo
> lorem ipsum hello foo

but when the word is at the end of line (ex with 'foo') it gives

lorem ipsum bar foo
> lorem ipsum barhello

So I have a

:nnoremap cP "_diwp

The question is how to "merge"

:nnoremap cp "_diwP
:nnoremap cP "_diwp

in one map ?

Thank you for help.

share|improve this question
up vote 0 down vote accepted

You can use ciw instead of diw and then an insert-mode paste:

:nnoremap cp "_ciw<C-r>"<Esc>

Or, always use P but only delete the original word afterward:

:nnoremap cp "_yiwPl"_de

"_yiw is to go to the beginning of the original word (b fails if the cursor is already on the first character of the word, and workarounds like lb also have pitfalls unless :set ve+=onemore).

share|improve this answer
I adopted :nnoremap cp "_yiwPl"_de Thanks a lot, I love your trick with "_yiw and I don't know <<de>> (but it's logic) – Hettomei Jun 27 '13 at 8:00
de is similar to dw but does not delete whitespace before the next word. – Nikita Kouevda Jun 27 '13 at 17:35

My ReplaceWithRegister plugin provides a gr mapping similar to yours that handles special cases like yours.

Also, you may be interested in the Smartput plugin; it changes the p / P commands to automatically adjust spaces, commas, etc. With it, you can also achieve your goal (though you'd have to use :nmap, not :nnoremap to enable the plugin in your mappings).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.