1

In vim, when highlighting and replacing individual words, I use:

viwy

Then, I go to the next word and type:

viwp

The default behavior is to immediately replace the yanked word with the replaced word. I figured this was advantageous, somehow, otherwise it would not have been implemented this way...and sure enough, after using it for a while, I like it.

However, after using it for a while, I have the memory for a mini stack: two words, the latest word in the register, and the previously yanked word.

How, in vim, would I implement a functionality where:

starting text: "hello world something"

viwyw ("hello" is yanked, cursor is at start of "world")
viwpw ("world" is replaced with hello, 
       "world" is now yanked, cursor is at "something")

and: 

tviwp (where 't' toggles or swaps the last two items yanked,
       and "something" is replaced by "hello")

resulting text: 

"hello hello hello"

And I do not lose the default behavior, so that I can still do this:

hello world 
^ cursor location

viwywviwpbviwp

result:

world hello
  • if i understand correctly, you could simply use viw"0p – Mass Jun 25 '18 at 22:26
3

Extending comment from @Mass, I want to say, the functionality you want can be used with these keys in order viwywviwpwviw"0p. Here, 0 is a register and "0 is motion to yank to or put from reg 0. See :h registers

  1. viwy would yank hello
  2. w would goto next word, world.
  3. viwp would visual-select the word world and put(paste) the content of register ", i.e. the word something. And the content of the register " would get changed to world.
  4. w would goto next word, something.
  5. viw"0p would visual-select the word something and put(paste) the content of the register 0, i.e. the word hello.

Now, that is an interesting matter. If you read through the help docs, you'll see that, the numbered register 0 contains the text from the most recent yank commands, unless another register was specified with ["x"]. So, when previously you yanked with viwy, you used the unnamed register. Then when you visual-selected a word and put the content of the reg ", the content of the reg " and the visual-selection of the buffer gets exchanged. But the numbered register 0 still holds the previously yanked word. This will remain the case until you yank with y or change the content of reg 0 in some other way (e.g. :let @0=blablabla).

So, overall, the register 0 holds the most recent unspecified yank with y, not when used c or d. Those operators change the content of registers 2-9.

I strongly suggest you read through the help docs of vim about the registers. In my opinion, they are the strongest side of vim over other editors, even stronger than modal editing.

Edit: I assumed that you know about vim text objects and motions. For example, you can do yiw instead of viwy. yiw translates into yank inner word.

  • 1
    Of course, viwy could become yiw to save keystrokes. – D. Ben Knoble Jun 26 '18 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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