To speak to others is to first silence those in whose name we speak. (Callon 1986)

More .emacs-fu

I promised somebody I know to start posting more .emacs-fu on in my blog. So here goes. First out is a function to duplicate whatever the line under the cursor is. If you are a programmer this is something that you probably do a lot. However C-a C-k C-y C-y or C-a M-spc C-e M-w gets old very fast. Instead, put the following function in your .emacs:

(defun duplicate()
 "Duplicate it the line we are on"
 (interactive)
 (let (
       (beg (line-beginning-position))
       (end (line-end-position)))
   (copy-region-as-kill beg end)
   (beginning-of-line)
   (forward-line 1)
   (yank)
   (newline)
   (forward-line -1)))

and bind it to any key-kombo you like (I used C-c C-j), and duplicating lines is suddenly just a key press away.

And while we are in the spirit. Another thing I often want to do is copy the whole line under the cursor so that I can paste it somewhere else in my document. Similarly add the following function to your .emacs:

(defun copy-line ()
  "Copy current line in the kill ring"
  (interactive)
  (kill-ring-save (line-beginning-position)
                  (line-beginning-position 2))
  (message "Line copied"))

and bind it to any key-kombo you like (I used C-c C-y), and copying a line is suddenly just a key press away.

Enjoy!

posted on 15 Jun 2010

About me

    • Languages of choice
    • C, python, perl and lisp
    • Current interests
    • GSM-networks and technology, Emacs, Stumpwm, org-mode, philosophy and ethics, microprocessors, Drupal, Latex and writing.