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

75,183

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
75,183

Related videos on Youtube

Michael Durrant
Author by

Michael Durrant

rails ruby rspec rock

Updated on September 18, 2022

Comments

  • Michael Durrant
    Michael Durrant over 1 year

    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.

  • tlo
    tlo over 9 years
    Option 3 (even simpler): With your word yanked, cursor over the first character of the word you want to replace and do vep.
  • eigenfield
    eigenfield over 5 years
    I like Option 3 when there is only 1 word to change. If there is more than one same word to change, Option 1 allows me to use the . operator to repeat it..
  • JKillian
    JKillian about 5 years
    ve"0p would work to let you repeat your change
  • Paul Parker
    Paul Parker about 5 years
    If you're going to map it to something, it's probably better to use: map <C-j> ciw<C-r>0<ESC> so that it doesn't matter from where in the word you press ^j.
  • Paul Parker
    Paul Parker about 5 years
    Anyone wanting to know why CTRL-R does it's magic: :help insert then search CTRL-R /CTRL-R. For how ve"0p works, look at :help registers
  • Paul Parker
    Paul Parker about 5 years
    ,rw would make more sense, and on QWERTY is probably a smidgen easier and faster to belt out. On DVORAK, I chose ,rc, which although it doesn't make much sense at all, it is easier and faster than all of them. On DVORAK, ,rw is a horror show ;)
  • Michael Draper
    Michael Draper almost 5 years
    Option 1 works amazingly for me, thank you so much for this answer!
  • eddd
    eddd almost 5 years
    I find the vi{range}p the best approach.
  • nils petersohn
    nils petersohn about 4 years
    you can avoid that navigation to the beginning of the word by using yiw or yaw which means yank inner word or yank all word.
  • Paul Rougieux
    Paul Rougieux over 3 years
    If you want to replace the first word of the line and in general if your cursor is on the first letter of the word, that should be Pldw: capital "P" to paste before the cursor, then move one character right, delete word.
  • azureai
    azureai about 3 years
    The problem with option 1 here is that when you're coming from another vim window, having stored what you want to paste in the * buffer, it gets overwritten (at least for me), by the 'ciw'.
  • Admin
    Admin almost 2 years
    I tried Option 1, works. But if I do a typo and correct it with backspace, the text to paste moves to the - register. Maybe I should have exited the insert mode and undone the changes, or keep track of the backspace and use Ctrl R -