bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#46621: Copy line


From: Simen Heggestøyl
Subject: bug#46621: Copy line
Date: Sat, 18 Jun 2022 11:32:06 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux)

Lars Ingebrigtsen <larsi@gnus.org> writes:

> I've now added this command to Emacs 29 as `duplicate-line'.

Cool! I've had a similar command defined locally since 2015 (which I've
found so useful over time that I've given it a short global keybinding).

I have some suggestions that I think make the command even more useful:

- Making it more DWIM'y by having it duplicate the region instead when
  it's active (perhaps giving it a more general name like
  `duplicate-thing`, `duplicate-dwim`, or maybe just `duplicate`).

- Having point end up at the duplicated line instead of the original
  one, since that's the line you usually want to edit (in my
  experience).

- With a prefix argument, comment out the original line/region after
  duplicating it. This is perhaps a bit too specialized to be useful in
  general compared to the other two suggestions, though I've found it
  very useful personally.

Happy to help if any of these sound eligible for inclusion.

Here's my local definition for reference:

(defun duplicate (arg)
  "Duplicate the current line, or region if active.
When called with a prefix argument the current line or region is
commented out before it's copied."
  (interactive "P")
  (setq arg (or arg 1))
  (let ((beg (if (region-active-p)
                 (region-beginning)
               (line-beginning-position)))
        (end (if (region-active-p)
                 (region-end)
               (line-end-position)))
        (point (point)))
    (goto-char end)
    (let ((to-duplicate (buffer-substring beg end)))
      (when (listp arg)
        (comment-region beg end)
        (setq arg 1))
      (dotimes (_ arg)
        (end-of-line)
        (newline)
        (insert to-duplicate)))
    (backward-char (- end point))))





reply via email to

[Prev in Thread] Current Thread [Next in Thread]