Friday, March 12, 2010

How to duplicate lines/regions in Emacs

So, I read a blog post just now on line/region duplication over here and thought I'd give it a try as well.

Here's what I came up with:


(defun duplicate-line-or-region (arg)
"Duplicates the current region (transient) ARG times, but at
least twice. If none is active, duplicate the current line
instead."
(interactive "p")
(let ((times (if (> arg 1) arg 2)))
(save-excursion
(if (use-region-p)
(kill-region (mark) (point))
(kill-region (point-at-bol) (point-at-bol 2)))
(dotimes (i times)
(yank)))))

No comments: