Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

So I usually find myself copying text from one point to another while overwriting old text where the new is pasted:

blah1
newtext
blah2
wrong1
blah3
wrong2
blah4

Suppose I visual-mark newtext and yank it. Now I select wrong1 (which could be anything, not necessarily just a word) and paste the newtext. However, if I now do the same with wrong2 it will be replaced with wrong1 instead of newtext.

So how do I keep the text that is in the buffer from being swapped with the text that I am currently overwriting?

Edit 1

Although I quite like the reigister suggestions (I think I will start using registers more, now that I discovered the :dis command), I am going with a modification of jinfield's answer, because I do not use the swapping mode.

vnoremap p "0p
vnoremap P "0P
vnoremap y "0y
vnoremap d "0d

does the trick perfectly.

Edit 2

I was too fast; romainl's solution is precisely what I was looking for, without the hack in Edit 1.
Actually, vnoremap p "_dP is enough!
So, changing accepted answer.

share|improve this question
    
I'd like to know the answer too! –  Nighthawk Aug 10 '11 at 19:42
add comment

6 Answers

up vote 6 down vote accepted

I have these mappings in my .vimrc:

" delete without yanking
nnoremap <leader>d "_d
vnoremap <leader>d "_d

" replace currently selected text with default register
" without yanking it
vnoremap <leader>p "_dP

"_ is the "blackhole register", according to :help "_:

"When writing to this register, nothing happens. This can be used to delete text without affecting the normal registers. When reading from this register, nothing is returned. {not in Vi}"

share|improve this answer
add comment

In addition to the standard buffer, you can yank text into named buffers, and then put from those named buffers. There are up to 26 named buffers you can use (one for each letter). Use double quotes and a letter to access a named buffer. Examples:

"dyy - Yank current line into buffer d.
"a7yy - Yank next seven lines into buffer a.
"dP - Put the contents of buffer d before cursor.
"ap - Put the contents of buffer a after cursor

Another cool thing, if you use a capital letter instead of lower case, i.e "Dyy the current line will be Appended to the buffer d instead of replacing it. More details in the O`Reilly book: http://docstore.mik.ua/orelly/unix/vi/ch04_03.htm

share|improve this answer
1  
Very cool thing. I knew about buffers, but didn't connect them with this problem. It is still cumbersome to "a everything, but okay. –  bitmask Aug 10 '11 at 20:27
add comment

When you yank the text into the unnamed register*, a copy is also put into register 0. Each time you replace selected text, you can just paste from the 0 register. See

:help registers

In addition, if you are replacing a number of words with the same word, you can just move to the start of the word to be replaced and type .. That will repeat the last editing operation. See

:help single-repeat

* The storage locations that you yank into and put from are called registers. A buffer is the thing that you edit, usually a copy of a file from disk.

share|improve this answer
add comment

When using put in visual mode, the text you're replacing, wrong1, is overwritten by the contents of the 'unamed' register.

This actually works by 'putting' the register after the selection and then deleting the selection. The problem is that this deletion is now stored in the unnamed register and will be used for the next put action.

The solution, according to :h v_p, is to yank into a named register, such as "0y, then paste using "0p as many time as you need. It may be helpful to map <leader>y and <leader>p to use a named register, if this is something you do frequently.

:map <leader>y "0y
:map <leader>p "0p

for more help see:

:help v_p
:help map
share|improve this answer
    
This solution seems most usable, until something clever comes up from the vim itself. –  Yugal Jindle Nov 13 '13 at 4:14
add comment

Pasting from "0 register is important to know, but you often want to replace many times. If you make it a repeatable action, you can use the . operator, as alluded to by garyjohn. It's explained on the vim wiki:

yiw     yank inner word (copy word under cursor, say "first". Same as above).
...     Move the cursor to another word (say "second").
ciw<C-r>0   select "second", then replace it with "first" If you are at the start of the word then cw<C-r>0 is sufficient.
...     Move the cursor to another word (say "third").
.   select "third", then replace it with "first". 
share|improve this answer
add comment

I need this so often, I wrote a plugin for that: ReplaceWithRegister.

This plugin offers a two-in-one gr command that replaces text covered by a {motion}, entire line(s) or the current selection with the contents of a register; the old text is deleted into the black-hole register, i.e. it's gone.

share|improve this answer
add comment

Your Answer

 
discard

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.