emacs, copy and cut current line

Perm url with updates: http://xahlee.org/emacs/emacs_copy_cut_current_line.html

Emacs: How to Copy/Cut Current Line

Xah Lee, 2010-05-21

Last month, i wrote Emacs: Shortcut to Delete Whole Line, because i find that deleting whole line is much frequently needed. But also, i didn't mention that i often find the need to copy the current line too. Usually, this meas moving the cursor to beginning of line, mark, move to end of line, then copy. This is 4 operations. The following code will make it just a single operation:

(defadvice kill-ring-save (before slick-copy activate compile)
  "When called interactively with no active region, copy the current line."
  (interactive
   (if mark-active
       (list (region-beginning) (region-end))
     (progn
       (message "Current line is copied.")
       (list (line-beginning-position) (line-end-position)) ) ) ))

(defadvice kill-region (before slick-copy activate compile)
  "When called interactively with no active region, cut the current line."
  (interactive
   (if mark-active
       (list (region-beginning) (region-end))
     (progn
       (message "Current line is cut.")
       (list (line-beginning-position) (line-end-position)) ) ) ))

With the above code, when you do not have a text selection, copy will just copy the current line. Similar for cut. Super!

Thanks to Joseph O'Donnell for mentioning this. The code is originally from emacswiki.org SlickCopy. Apparently, this behavior is default in VisualStudio, TextMate, SlickEdit.

Was this page useful? If so, please do donate $3, thank you donors!

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs