If you want to yank something N times, the simplest approach is probably to use a keyboard macro: See https://www.gnu.org/software/emacs/manual/html_node/emacs/Keyboard-Macros.html.
For example, mark and copy the text, then:
F3 C-y F4
; record a keyboard macro that does the yank
C-u 20 C-x e
; run last macro 20 times
Why didn't your approach work?
Your assumption here is incorrect:
Alt-2 Ctrl-X z (repeat last two commands)
From the doc for repeat-command
:
Repeat most recently executed command. If REPEAT-ARG is non-nil (interactively, with a prefix argument),
supply a prefix argument to that command. Otherwise, give the
command the same prefix argument it was given before, if any.
So C-x z
will only repeat the last command. In your example you are calling yank
with prefix arg 2. Checking what yank
does with that:
With argument N, reinsert the Nth most recent kill.
So actually you are yanking the 2nd most recent kill. Try this to see it work. Start with a buffer with this and point at the beginning:
one
two
Mark and kill one and then two (with trailing newline), e.g.
C-SPC C-n M-w
; mark, go down a line, copying one
C-SPC C-n M-w
; same again, copying two
C-y
; yank (most recent)
M-2 C-x z
; repeat with argument 2 (yank 2nd most recent)
And the end result is this:
one
two
two
one
C-SPC
C-n
does not move down a line in my case, it selects the word and positions the cursor at the end of the line. – Dec 07 '22 at 22:38C-SPC
at the beginning of the word, thenC-n
to move to after the newline, thenM-w
. – NickD Dec 07 '22 at 22:42repeat
with an argument does not repeat N times. See https://www.gnu.org/software/emacs/manual/html_node/emacs/Repeating.html and the answer I've added that shows what it actually does. – glucas Dec 09 '22 at 15:17