Created
February 25, 2015 17:40
-
-
Save stephane/5e614da08649e59ef3c6 to your computer and use it in GitHub Desktop.
Duplicate lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun duplicate-current-line-or-region (arg) | |
"Duplicates the current line or region ARG times. | |
If there's no region, the current line will be duplicated. However, if | |
there's a region, all lines that region covers will be duplicated." | |
(interactive "p") | |
(let (beg end (origin (point))) | |
(if (and mark-active (> (point) (mark))) | |
(exchange-point-and-mark)) | |
(setq beg (line-beginning-position)) | |
(if mark-active | |
(exchange-point-and-mark)) | |
(setq end (line-end-position)) | |
(let ((region (buffer-substring-no-properties beg end))) | |
(dotimes (i arg) | |
(goto-char end) | |
(newline) | |
(insert region) | |
(setq end (point))) | |
(goto-char (+ origin (* (length region) arg) arg))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment