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.
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."
(interactive "p")
(unless n
(setq n 1))
(cond
;; Duplicate rectangle.
((bound-and-true-p rectangle-mark-mode)
(rectangle--duplicate-right n)
(setq deactivate-mark nil))
;; Duplicate (contiguous) region.
((use-region-p)
(let* ((beg (region-beginning))
(end (region-end))
(text (buffer-substring beg end)))
(save-excursion
(goto-char end)
(dotimes (_ n)
(insert text))))
(setq deactivate-mark nil))
;; Duplicate line.
(t (duplicate-line n))))