0

In emacs off late I have had to write some lines of code which are quite repetitive in nature. Consecutive lines are almost exactly the same except for some trivial modifications.

Is there a keystroke in emacs by which the line the cursor (point) currently on gets duplicated onto the next line. To generalize this, if I could specifiy which line number onto which I want to paste the current line, that would also be great.

smilingbuddha
  • 14,334
  • 33
  • 112
  • 189
  • 2
    This doesn't answer your question, but you should refactor the duplication out of your code, or attempt to reduce the duplication as much as you can. If you just leave it, you'll be doing this same repetitive task in the future. – anio Aug 26 '12 at 18:12
  • 2
    Previously answered here: http://stackoverflow.com/questions/88399/how-do-i-duplicate-a-whole-line-in-emacs It sounds like you're writing program source code. Wherever you frequently repeat program source code, it might be better to use a function or macro. – minopret Aug 26 '12 at 18:13

3 Answers3

1

I tend to use the following:

Select a region, and then C-x r x. Where r is the name of the register; a single character on your keyboard can serve as the register.

Then C-x g r where you want to insert whatever line you saved in register r.

You can bind these 2 commands to shortcut keys to make your task even easier.

The benefit of using registers is that you can save different lines in different registers and recall them at any time. Also if you do C-k and kill a line, it won't interfere with your saved registers.

I hope that helps.

anio
  • 8,903
  • 7
  • 35
  • 53
1

This works, tweak until it's prefect for you. Alternatively, just use a macro :)

(defun copy-line ()
  "copies a line"
  (interactive)
  (move-beginning-of-line 1)
  (kill-line 1)
  (yank)
  (yank)
  (setq kill-ring (cdr kill-ring)))
Haakon Løtveit
  • 1,009
  • 10
  • 18
0

You can try with emacs macro. C-X ( C-x ). More information can be found: M-: (info "(emacs)Keyboard Macros") RET

mathk
  • 7,973
  • 6
  • 45
  • 74