6

I am copying a word with: viwy (visual, inner, word, yank) Now I would like to replace some other word: viwp.

This works only once, because each word is replaced by the last removed word.

Ok, so I use yanked register: viw"0p

There is only one problem: when I try to repeat that command using the dot operator, it does not work! I have to write viw"0p again which is cumbersome.

Why does the shortcut not work? How to make it work?

1

1 Answer 1

2

Why does the shortcut not work?

The dot operator '.' repeats the last change, not the last keystrokes/command, as mentioned in this question.

The dot operator repeats the last simple change. viwp is no longer a simple change, because it's composed of a paste and a delete. From :help put-Visual-mode

                                                put-Visual-mode v_p v_P
When using a put command like p or P in Visual mode, Vim will try to
replace the selected text with the contents of the register.  Whether this
works well depends on the type of selection and the type of the text in the
register.
...
(Implementation detail:
it actually works by first putting the register after the selection and then
deleting the selection.)

Your put viwp actually works by first putting the register after the selection and then deleting the selection. So the dot operator saves the "delete the selection" command, and repeats that. Not the put.

You can see the contents of the registers at any time by passing a list of arguments to the :register command. For example, to see "", "0, "1, "- and ". register contents: :register "01-.

How to make it work?

I would use a macro recording, not the dot operator

If I understand your request correctly, you want to change these words

first
second
do not change
third
do not change
fourth
do not change

to this

first
first 
do not change
first
do not change
first
do not change

To do that, I would

  1. viw"ay to yank that word "first" to register "a
  2. motion to the word to replace. In my example, this word is 'second'
  3. qqviw"apq to record to register "q the keystrokes viw"ap. This will:
    1. visually select the word, then
    2. put the content of register "a over that selected word, then
    3. stop recording
  4. move to the next word to replace, type @q to execute the contents of register "q as an Ex command.
  5. move to the next word, type @@ to repeat the previous @ command.

@@ is not . Maybe someone else can come up with something that uses less keystrokes. I look forward to learning what that is.


2
  • Is there a reason you select words in visual mode instead of sticking with normal mode? For example, I would expect the first one could be done with "ayw.
    – zondo
    Apr 23, 2017 at 2:59
  • @zondo I simply followed the lead of the original poster. Apr 23, 2017 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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