Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs equivalent of Vim's yy10p?

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?

Any ideas before I go and write my own functions.

like image 982
Sard Avatar asked Sep 16 '08 13:09

Sard


3 Answers

you can use a keyboard macro for that:-

C-a C-k C-x ( C-y C-j C-x ) C-u 9 C-x e

Explanation:-

  • C-a : Go to start of line
  • C-k : Kill line
  • C-x ( : Start recording keyboard macro
  • C-y : Yank killed line
  • C-j : Move to next line
  • C-x ) : Stop recording keyboard macro
  • C-u 9 : Repeat 9 times
  • C-x e : Execute keyboard macro
like image 137
ljs Avatar answered Nov 01 '22 03:11

ljs


Copying:

If you frequently work with lines, you might want to make copy (kill-ring-save) and cut (kill-region) work on lines when no region is selected:

(defadvice kill-ring-save (before slickcopy activate compile)
  "When called interactively with no active region, copy a single line instead."
  (interactive
   (if mark-active (list (region-beginning) (region-end))
     (list (line-beginning-position)
           (line-beginning-position 2)))))
(defadvice kill-region (before slickcut activate compile)
  "When called interactively with no active region, kill a single line instead."
  (interactive
   (if mark-active (list (region-beginning) (region-end))
     (list (line-beginning-position)
           (line-beginning-position 2)))))

Then you can copy the line with just M-w.

Pasting:

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 kmacro-end-and-call-macro instead (that's C-u 9 C-x e or even just C-9 C-x e or M-9 C-x e).

Another way: You can also just stay in M-x viper-mode and use yy10p :)

like image 11
ShreevatsaR Avatar answered Nov 01 '22 02:11

ShreevatsaR


You may know this, but for many 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".

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.

Also C-x z, "repeat last command" seems to be immune to C-u.

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.

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.

Had I a vote, I'd vote for kronoz answer.

like image 11
Baxissimo Avatar answered Nov 01 '22 03:11

Baxissimo