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
viw"ay
to yank that word "first" to register "a
- motion to the word to replace. In my example, this word is 'second'
qqviw"apq
to record to register "q
the keystrokes viw"ap
. This will:
- visually select the word, then
- put the content of register
"a
over that selected word, then
- stop recording
- move to the next word to replace, type
@q
to execute the contents of register "q
as an Ex command.
- 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.