<p>How can I copy a line 10 times easily in Emacs?  I can't find a copy-line shortcut or function.  I can use C-aC-spcC-eM-w to laboriously copy the line but how can I then paste it more than once?</p>

<p>Any ideas before I go and write my own functions.</p>

[{'answer_id': 72008, 'author': 'mmattax', 'author_id': 1638, 'author_profile': 'https://Stackoverflow.com/users/1638', 'pm_score': 0, 'selected': False, 'text': '<p>You will want to kill the line: C-a C-k, and then C-y or ?</p>\n'}, {'answer_id': 72039, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 5, 'selected': False, 'text': '<p>you can use a keyboard macro for that:-</p>\n\n<p>C-a C-k C-x ( C-y C-j C-x ) C-u 9 C-x e</p>\n\n<p><strong>Explanation:-</strong></p>\n\n<ul>\n<li>C-a   : Go to start of line</li>\n<li>C-k   : Kill line</li>\n<li>C-x ( : Start recording keyboard macro</li>\n<li>C-y   : Yank killed line</li>\n<li>C-j   : Move to next line</li>\n<li>C-x ) : Stop recording keyboard macro</li>\n<li>C-u 9 : Repeat 9 times</li>\n<li>C-x e : Execute keyboard macro</li>\n</ul>\n'}, {'answer_id': 72141, 'author': 'Mike Monette', 'author_id': 6166, 'author_profile': 'https://Stackoverflow.com/users/6166', 'pm_score': 0, 'selected': False, 'text': "<p>I don't know of a direct equivalent (C-y 10 times is the best I know), but you may be interested in Viper, which is a vi emulation package for emacs.  It's part of the standard emacs distribution.</p>\n"}, {'answer_id': 72181, 'author': 'ShreevatsaR', 'author_id': 4958, 'author_profile': 'https://Stackoverflow.com/users/4958', 'pm_score': 5, 'selected': True, 'text': '<p><strong>Copying</strong>:</p>\n\n<p>If you frequently work with lines, you might want to make copy (<code>kill-ring-save</code>) and cut (<code>kill-region</code>) work on lines when no region is selected:</p>\n\n<pre><code>(defadvice kill-ring-save (before slickcopy activate compile)\n  "When called interactively with no active region, copy a single line instead."\n  (interactive\n   (if mark-active (list (region-beginning) (region-end))\n     (list (line-beginning-position)\n           (line-beginning-position 2)))))\n(defadvice kill-region (before slickcut activate compile)\n  "When called interactively with no active region, kill a single line instead."\n  (interactive\n   (if mark-active (list (region-beginning) (region-end))\n     (list (line-beginning-position)\n           (line-beginning-position 2)))))\n</code></pre>\n\n<p>Then you can copy the line with just M-w.</p>\n\n<p><strong>Pasting</strong>:</p>\n\n<p>Often a prefix argument just performs an action multiple times, so you\'d expect C-u 10 C-y to work, but in this case C-y uses its argument to mean which element of the kill-ring to "yank" (paste). The only solution I can think of is what kronoz says: record a macro with C-x ( C-y C-x ) and then let the argument of C-u go to <code>kmacro-end-and-call-macro</code> instead (that\'s C-u 9 C-x e or even just C-9 C-x e or M-9 C-x e).</p>\n\n<p><strong>Another way</strong>:\nYou can also just stay in <code>M-x viper-mode</code> and use yy10p :)</p>\n'}, {'answer_id': 72202, 'author': 'ddaa', 'author_id': 11549, 'author_profile': 'https://Stackoverflow.com/users/11549', 'pm_score': 2, 'selected': False, 'text': '<p>The only way I know to repeat arbitrary commands is to use the "repeat by argument" feature of keyboard macros.</p>\n\n<p>C-a C-space down M-w C-x ( C-y C-x ) C-9 C-x e</p>\n\n<ul>\n<li>C-a : Go to start of line</li>\n<li>C-space : Set mark</li>\n<li>down : Go to start of following line</li>\n<li>M-w : Copy region</li>\n<li>C-x ( : Start keyboard macro</li>\n<li>C-y : Yank copied line</li>\n<li>C-x ) : End keyboard macro</li>\n<li>C-9 C-x e : Execute keyboard macro nine times.</li>\n</ul>\n\n<p>That\'s kind of weak compared to vim. But only because vim is amazingly efficient at this sort of thing.</p>\n\n<p>If you are really pining for modal vi-like interaction, you could use one of the vi emulation modes, such as viper-mode. Check in the section "Emulation" of online emacs manual.</p>\n'}, {'answer_id': 72601, 'author': 'Baxissimo', 'author_id': 9631, 'author_profile': 'https://Stackoverflow.com/users/9631', 'pm_score': 4, 'selected': False, 'text': '<p>You may know this, but for <strong>many</strong> commands a "C-u 10" prefix will do the trick.  Unfortunately for the C-y yank command, "C-u" is redefined to mean "go back that many items in the kill ring, and yank that item".</p>\n\n<p>I thought you might be able to use the copy-to-register and insert-register commands with the C-u prefix command, but apparently that doesn\'t work either.</p>\n\n<p>Also C-x z, "repeat last command" seems to be immune to C-u.</p>\n\n<p>Another thought would be to use M-: to get an Eval prompt and type in a bit of elisp.  I thought something like (dotimes \'10 \'yank) might do it, but it doesn\'t seem to.</p>\n\n<p>So it looks like using C-u on a macro may indeed be the best you can do short of writing your own little function.</p>\n\n<p>Had I a vote, I\'d vote for kronoz answer.</p>\n'}, {'answer_id': 73678, 'author': 'cjm', 'author_id': 8355, 'author_profile': 'https://Stackoverflow.com/users/8355', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s a function I took from an OS/2 port of Emacs.  (Yes, I\'ve been using Emacs for a while.)</p>\n\n<pre><code>;; Author: Eberhard Mattes &lt;mattes@azu.informatik.uni-stuttgart.de&gt;\n(defun emx-dup-line (arg)\n  "Duplicate current line.\nSet mark to the beginning of the new line.\nWith argument, do this that many times."\n  (interactive "*p")\n  (setq last-command \'identity) ; Don\'t append to kill ring\n  (let ((s (point)))\n    (beginning-of-line)\n    (let ((b (point)))\n      (forward-line)\n      (if (not (eq (preceding-char) ?\\n)) (insert ?\\n))\n      (copy-region-as-kill b (point))\n    (while (&gt; arg 0)\n      (yank)\n      (setq arg (1- arg)))\n    (goto-char s))))\n</code></pre>\n\n<p>I have that bound to F9 d:</p>\n\n<pre><code>(global-set-key [f9 ?d]    \'emx-dup-line)\n</code></pre>\n\n<p>Then I\'d use C-u 10 F9 d to duplicate a line 10 times.</p>\n'}, {'answer_id': 75430, 'author': 'jfm3', 'author_id': 11138, 'author_profile': 'https://Stackoverflow.com/users/11138', 'pm_score': -1, 'selected': False, 'text': "<p>You get the line with C-k, you make the next command happen ten times with C-u 10, then you paste the line with C-y.  Pretty simple.</p>\n\n<p>If you always want C-k to do the whole line, you can set kill-whole-line to t.  No more fiddling with C-a or C-e.</p>\n\n<p>There's a lot you can do with fancy kill rings, registers, and macros, and I encourage you to learn them, but yanking a line ten times doesn't have to be tough or strange.</p>\n"}, {'answer_id': 1567969, 'author': 'Paul', 'author_id': 190056, 'author_profile': 'https://Stackoverflow.com/users/190056', 'pm_score': 3, 'selected': False, 'text': "<p>You don't need both C-x ) and C-x e in this example.</p>\n\n<p>You can just give the repeat argument straight to C-x ).  This stops recording and repeats the macro, in one step.  Or you can skip C-x ) and go straight to C-x e, since C-x e will end the recording before doing the repeats.</p>\n\n<p>Which way to choose depends on how you like your repeat count to work.  For C-x ) you say how many repeats you wanted in total (so 10 in this case).  For C-x e you need to say how many more repeats are left (i.e. 9).</p>\n\n<hr>\n\n<p>C-a C-k C-k will also kill the trailing newline, so you don't have to put it back yourself later.  It's quicker than using the mark, and doesn't need you to change any variables.</p>\n\n<p>Even better (unless you're in a terminal), you can use C-S-Backspace* to kill the entire line, regardless of where you are in it.</p>\n\n<p>[* If you're using X windows, make sure to type shift (<strong>not</strong> alt) or you may terminate your session!]</p>\n\n<p>Speaking of terminals, M-9 is a nice alternative if you find you can't type C-9.</p>\n\n<hr>\n\n<p>In Emacs 22 and higher, by default F3 starts a macro and F4 end/repeats a macro.  You just hit F3 to start recording, hit F4 when you're done, and hit F4 again to repeat the macro.  (F4 also takes an argument.)</p>\n\n<hr>\n\n<p>Putting this all together, to get 10 copies of the current line:</p>\n\n<ul>\n<li>C-S-Backspace : kill this line</li>\n<li>F3 : start macro</li>\n<li>C-y : yank the line</li>\n<li>C-1 C-0 F4 : make that 10 yanks</li>\n</ul>\n\n<p>Not quite as short as y y 10 p, but pretty close. :)</p>\n"}, {'answer_id': 1641331, 'author': 'quodlibetor', 'author_id': 25616, 'author_profile': 'https://Stackoverflow.com/users/25616', 'pm_score': 0, 'selected': False, 'text': '<p>Based on Baxissimo\'s answer I defuned this:</p>\n\n<pre><code>(defun yank-n-times (arg)\n  "yank prefix-arg number of times. Not safe in any way."\n  (interactive "*p")\n  (dotimes \'arg (yank)))\n</code></pre>\n\n<p>Set that to some key, call it with a prefix argument, and off you go.</p>\n\n<p><strong>edit</strong> (also modified the interactive call above to be less lousy)</p>\n\n<p>Or, here\'s a version that can sort of replace yank-pop:</p>\n\n<pre><code>(defun yank-n-times (&amp;optional arg)\n  "yank prefix-arg number of times. Call yank-pop if last command was yank."\n  (interactive "*p")\n  (if (or (string= last-command "yank")\n      (string= last-command "yank-pop"))\n      (yank-pop arg)\n    (if (&gt; arg 1)\n    (dotimes \'arg (yank))\n      (message "Previous arg was not a yank, and called without a prefix."))))\n</code></pre>\n\n<p>the message is kind of a lie, but you shouldn\'t call it without a prefix of greater than 1 anyway, so.</p>\n\n<p>Not sure if it\'s a good idea, but I replaced M-y with this, I\'ll see how that goes.</p>\n'}, {'answer_id': 74419566, 'author': 'Sergey V', 'author_id': 13256934, 'author_profile': 'https://Stackoverflow.com/users/13256934', 'pm_score': 0, 'selected': False, 'text': '<p>First you need this key binding in your .emacs:</p>\n<pre><code>;; yank n times\n(global-set-key &quot;\\C-y&quot; (lambda (n) (interactive &quot;*p&quot;) (dotimes (i n) (clipboard-yank))))\n</code></pre>\n<p>Then you can do:</p>\n<pre><code>C-a C-SPC C-n M-w C-u 10 C-y\n</code></pre>\n<p>C-a C-SPC C-n M-w - select whole line<br>\nC-u 10 C-y - repeat &quot;clipboard-yank&quot; 10 times</p>\n'}]
['https://Stackoverflow.com/questions/71985', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9831/']