2

I'm just not able able to replicate the answer on this. Starting with:

lipsum

I do (my interpretaton):

  1. Alt-W over the word (copy)
  2. Enter (new line)
  3. Ctrl-Y (paste)
  4. Alt-2 Ctrl-X z (repeat last two commands)

What I expected:

lipsum
lipsum
lipsum

What I get:

lipsum
lipsumlipsum

How did I do wrong?

PS1: breakdown of 1.

  • C-SPC before lipsum
  • C-E
  • M-W

Effect: selected region=lipsum(cursor at end of line).

2 Answers2

2

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:

  1. F3 C-y F4 ; record a keyboard macro that does the yank
  2. 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.

  1. C-SPC C-n M-w ; mark, go down a line, copying one
  2. C-SPC C-n M-w ; same again, copying two
  3. C-y ; yank (most recent)
  4. M-2 C-x z ; repeat with argument 2 (yank 2nd most recent)

And the end result is this:

one
two
two
one
glucas
  • 20,563
  • 1
  • 54
  • 84
1

You copied only a word, not that word plus the newline character that follows it.

You then yanked the copied word twice. The yanked words aren't separated by newline chars because you didn't include such a char in what you copied to the kill-ring.

  1. C-SPC before the word you want to copy.

  2. Then C-n to move down a line (to the beginning of the next line).

  3. Then M-w to copy the word and newline.

  4. Then do your yanking (whether by repeating or not).

Drew
  • 77,472
  • 10
  • 114
  • 243
  • "You copied only a word, not that word plus the newline character that follows it." But I repeated the last two commands. The sequence 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:38
  • 1
    That's probably because there is no newline at the end of the word. Insert one and then do C-SPC at the beginning of the word, then C-n to move to after the newline, then M-w. – NickD Dec 07 '22 at 22:42
  • The problem is repeat 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
  • @glucas: Good point. – Drew Dec 09 '22 at 15:54