If there is something you find yourself doing over and over again in emacs, you can create a shortcut. For example, something I find myself doing repeatedly is duplicating a line and making a modification on the second line.
int v1 = 1; int v2 = 2;
The way I used to do this is:
- I moved to the beginning of the line
- I set the marker
- I moved to the end of the line
- I copied the marked region
- I pressed enter
- and finally I yanked the copied line
There may be a quicker way, even in vanilla emacs. However, this doesn’t take long, but it is still better to have it available with a single keypress.
(defun duplicate-current-line () (interactive) (beginning-of-line nil) (let ((b (point))) (end-of-line nil) (copy-region-as-kill b (point))) (beginning-of-line 2) (open-line 1) (yank) (back-to-indentation)) (global-set-key "\C-cd" 'duplicate-current-line)
This does have a few minor problems – e.g. it doesn’t work if we are on the last line of a buffer. Fixing this is left as an exercise for the reader.