Wednesday 15 April 2009

Emacs - Copying a whole line without killing it

For some reason I always find myself trying to copy a whole line and then pasting back somewhere else. I have never found a good way in Emacs to do this except for <home> C-k C-y until today, when I wrote this little function to do it for me and bound it to M-k because I never use kill-sentence anyway. (the excessive commenting is for people learning Lisp like me)
;Create a COPY line function
(defun copy-total-line ()
  "Copy the whole line that the cursor is on"
  (interactive)
   ;We want to return to where we started
  (save-excursion      
    ;Jump to the start of the line          
    (beginning-of-line)           
    ;Store the start of the line         
    (let* ((startpos (point)))    
      ;Move to the end of the line
      (end-of-line)
      ;Add it all to the kill ring as M-w would                       
      (kill-ring-save startpos (point) )  
      )
    )
  )
;Bind it to M-k (cause I never use kill sentence)
(define-key global-map "\M-k" 'copy-total-line)
On further searching the Emacs wiki has some other functions to do this, but this way I learned some lisp!

No comments: