Skip to content

Instantly share code, notes, and snippets.

@stephane
Created February 25, 2015 17:40
Show Gist options
  • Save stephane/5e614da08649e59ef3c6 to your computer and use it in GitHub Desktop.
Save stephane/5e614da08649e59ef3c6 to your computer and use it in GitHub Desktop.
Duplicate lines
(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