Function: duplicate-dwim
duplicate-dwim is an autoloaded, interactive and byte-compiled
function defined in misc.el.gz.
Signature
(duplicate-dwim &optional N)
Documentation
Duplicate the current line or region N times.
If the region is inactive, duplicate the current line (like duplicate-line).
Otherwise, duplicate the region, which remains active afterwards.
If the region is rectangular, duplicate on its right-hand side.
Interactively, N is the prefix numeric argument, and defaults to 1.
The variables duplicate-line-final-position and
duplicate-region-final-position control the position of point
and the region after the duplication.
Probably introduced at or before Emacs version 29.1.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/misc.el.gz
;;;###autoload
(defun duplicate-dwim (&optional n)
"Duplicate the current line or region N times.
If the region is inactive, duplicate the current line (like `duplicate-line').
Otherwise, duplicate the region, which remains active afterwards.
If the region is rectangular, duplicate on its right-hand side.
Interactively, N is the prefix numeric argument, and defaults to 1.
The variables `duplicate-line-final-position' and
`duplicate-region-final-position' control the position of point
and the region after the duplication."
(interactive "p")
(unless n
(setq n 1))
(cond
((<= n 0) nil)
;; Duplicate rectangle.
((bound-and-true-p rectangle-mark-mode)
(rectangle--duplicate-right n
(if (< duplicate-region-final-position 0)
n
duplicate-region-final-position))
(setq deactivate-mark nil))
;; Duplicate (contiguous) region.
((use-region-p)
(let* ((beg (region-beginning))
(end (region-end))
(text (buffer-substring beg end))
(pt (point))
(mk (mark)))
(save-excursion
(goto-char end)
(duplicate--insert-copies n text))
(let* ((displace (if (< duplicate-region-final-position 0)
n
duplicate-region-final-position))
(d (* displace (- end beg))))
(unless (zerop d)
(push-mark (+ mk d))
(goto-char (+ pt d)))))
(setq deactivate-mark nil))
;; Duplicate line.
(t (duplicate-line n))))