How can I do a 'change word' in Vim using the current paste buffer?

I have some text in my paste buffer, e.g. I did a yw (yank word) and now I have ‘foo’ in my buffer.

I now go to the word ‘bar’, and I want to replace it with my paste buffer.

To replace the text manually I could do cw and then type the new word.

How can I do a ‘change word’, but use the contents of my paste buffer instead of manually typing out the replacement word?

The best option I have right now is to go to the beginning of the word I want to replace and do dw (delete word), go to the other place, and do the yw (yank word). Then go back to the replacement area and do p (paste) which is kind of clumsy, especially if they are not on the same screen.

Asked By: Michael Durrant

||

Option 1

You could use registers to do it and make a keybinding for the process.

Yank the word you want to replace with yw.

The yanked word is in the 0 register which you can see by issuing :registers.

Go to the word you want to replace and do cw. Do Ctrl+r followed by 0 to paste the 0 register.

The map for that would look something like this (assuming Ctrl+j as our key combo):

:map <C-j> cw<C-r>0<ESC>

Option 2 (simpler)

With your word yanked, cursor over the word you want to replace and do viwp. Which is visual select inner word and paste.

Courtesy of @tlo in the comments: you could also just do vep. One char shorter. Downside have to position cursor at start of word and (as with mine) changes the buffer.

Comment (from Michael):

This is good. Extra note: the second method is indeed easier but, as is, only works for ONE substitution because after each substitution the buffer then gets changed to the field that was replaced (old text). The first method is a little harder to use BUT has the advantage that the buffer 0 stays ‘as is’ so you can use that method to do more than 1 replacement of the same text.

Answered By: jmathew

yw to yank your word, then move the cursor to the word you wish to replace and use "_dw to delete it, sending the text to the null register (so it doesn’t overwrite the contents of the " register, where yanked/cut text goes by default), and then simply paste with p.

You could use the following mapping to make things a little easier:

nnoremap <leader>d "_d

…so in normal mode, you could use dw to delete a word, without affecting the " register.

Answered By: evilsoup

I need this so often, I wrote a plugin to simplify and allow maximum speed: ReplaceWithRegister.

This plugin offers a two-in-one gr command that replaces text covered by a {motion} / text object, 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. It transparently handles many corner cases and allows for a quick repeat via the standard . command. Should you not like it, its page has links to alternatives.

Answered By: Ingo Karkat

To repeat the paste use the change command.

cw ^R0ESC (^R=Control+R; 0 for register 0)

will replace the word with the contents from reg. 0 and let’s you repeat it with .

This is great for cgn (VIM 7.4) to replace search patterns:

  • First search with / or *
  • cgn ^R0ESC
  • . to repeat on next match or n to skip
Answered By: laktak

Since it’s not included yet, you usually replace strings with :%s/.../.../ and you can do this with the word under your cursor as well.

  1. You enter the first part of the command, what you are searching for: :s/find/
  2. Then you click <ctrl-r> and choose with the cursor the word you want to replace it with.
  3. Then you click <ctrl-w> (small “word”) or <ctrl-a> (big “word) to paste the word under the cursor into the command line
  4. Then you finish the command.

See the link about how to use cursor stuff in the command line.

Answered By: erikbstack

I use this plugin for expanding selection region:
https://github.com/terryma/vim-expand-region

So my combination for changing a word will be: vvpin any place of a word.

I also have a hack for saving unnamed register contents after paste so I can repeat this.

vnoremap p p:let @"=@0 <CR>
vnoremap P P:let @"=@0 <CR>

(my clipboard is autoselect)

Answered By: user2656799

I usually use this: yw to yank foo, then delete bar with dw, and as final step, put foo from register 0 which holds yanked text using "0p. Repeat putting with period (.) as needed.

Also, instead of yw and dw, you can use yiw and diw (notice the extra i – it means inside). With that you can yank or delete word from any location inside it, not just on the beginning of word.

Answered By: Nino

I’m using below key mapping to replace the current word with copied word.
" replace with Register 0
map <leader>rr ciw<C-r>0<Esc>

yiw copy a word. Navigate to a word. ,rr replace the word.
Leader key is map to ,.

Answered By: Fisher

First, navigate to the start of the word you want to yank. If you’re in the middle of the word, just do b to go back to the start of it. Then do ye to yank the whole word, without any trailing spaces (yank to end). Finally, go to the word you want to replace – perhaps by doing w a few times – and do a vep to enter visual selection mode, select to the end of the word, and paste the contents last yanked.

(If anyone wants to know why I didn’t simply comment on the accepted answer, for the small modification of using ye rather than yw, it’s because my reputation on this stack is still too low.)

Answered By: Anomaly
  1. use yw to yank the word, go to the word you want to change.
  2. use v to select the word, ve or vw depending on your situation.
  3. enter p to paste the word you copied to replace the selected block.
Answered By: ramwin

The Vim way is to learn how to intentionally use the yank, delete and other registers. Once you know these, you will easily find your own key sequences to do this.

Register "0 is the yank register. Anything you yank will be put here, but neither deletes or change removals will ever touch register "0.

So, in your example, you had just yanked a word. To replace a word with what you just yanked, you take advantage of deletions never touching the yank register. Move to the target word, delete it with dw, then paste from your yank register with "0p or better yet, cw then ^R0 (which is repeatable).

The flip side to the yank register is the small deletions register "-. Any small delete or change removal is put here, but yanks never touch "-. A deletion counts as small if it is less than a full line.

Registers "1"9 are the large delete history registers. With "1 containing the latest large deletion or change removal, and "9 containing the oldest large deletion or change removal. Only deletes that aren’t small, i.e. deletes of one full line or more, get pushed onto "1"9.

For any operation that changes a register, a copy is also always placed in the default, a.k.a. unnamed register "". This is the register used when you don’t explicitly name a register.

Register "_ is the black hole register, and it is always empty. If you delete to it, nothing in any register is changed at all, not even the default "" register or the black hole register itself. The removed text is fully gone, apart from your undo history. Yanking to or pasting from the black hole register does essentially nothing.

The black hole register "_ lets you do things like first one small deletion, then a number of other deletions into "_ without changing your small deletions register "-, then paste your first small deletion.

You are probably already familiar with the normal named registers, they are simply registers "a"z, and you can use them as scratch space at will. If you refer to a named register by its capital form, "A"Z, you will append to it rather than replace its contents.

Other registers are the last inserted register "., the filename registers "% and "#, the command register ":, search register "/ and expression register "=.

You can get a list of all these registers as well as their contents with the command :register. Because it shows the current contents of the registers, the command is very useful to experiment with and learn what ends up where.

Answered By: 00prometheus

Adding to the other better answers. A bit too obvious but you could simply paste before you delete.
’yw’ then go the word you wanna replace, ’pldw’ (paste, move one character right, delete word.

Answered By: Ali Almoullim

Go to visual mode by pressing v to capture the interesting text, and copy it by pressing y. Now capture the text you want delete, and press p for pasting.

Replacing text in Vim

Answered By: Purkhalo Alex

My tip/suggestion: vim-exchange plugin. it helps to exchange words like this:

Mark the first word for exchange -> cxiw, then press dot ..

If you are using vim-plug just use:

Plug 'tommcdo/vim-exchange'

Then

:PlugInstall

In case of other plugin manager follow the documentation

Answered By: SergioAraujo

Add the below lines to your vimrc file.

"Change" without buffering.

nnoremap c "_c
vnoremap c "_c

Bonus

"Delete" without buffering.

nnoremap d "_d
vnoremap d "_d

The below 2 lines enable vim & system copy/pasting to interwork. EX: if you copy text from the browser you can paste it using the P command. And vice versa.

set clipboard=unnamed
set clipboard=unnamedplus
Answered By: Saif Hakeem

For Neovim, I’m using yw to yank a word. This puts the word in the "0 register.

Then, I use this mapping

keymap.set("n", "<leader>rw", 'diw"0P')

to replace the word the cursor is currently at with the word that I yanked.

Answered By: Tim Skov Jacobsen
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.