The command kill-whole-line
does just what it says. With a prefix argument, it allows me to kill multiple lines at the same time.
How can I get the same functionality, but with copying instead of killing?
The command kill-whole-line
does just what it says. With a prefix argument, it allows me to kill multiple lines at the same time.
How can I get the same functionality, but with copying instead of killing?
Here's a simple function, slightly adapted from the Copying Whole Lines node on EmacsWiki:
(defun copy-whole-line (&optional arg)
"Like `kill-whole-line', but copy instead."
(interactive "P")
(let ((buffer-read-only t)
(kill-whole-line t)
(kill-read-only-ok t))
(save-excursion (kill-whole-line arg))
(message ""))) ; clear minibuffer (not strictly necessary)
Install and activate the whole-line-or-region package. Now your M-w and C-w will work the way you want (including prefix arguments) when there is no region active.
(defun copy-multiple-lines (&optional arg)
"replace default `copy-line` function to copy whole lines to the kill ring.
With prefix argument ARG, copy that many lines including current one.
Negative arguments copy lines backward including current one.
If ARG is zero, copy current line but exclude the trailing newline."
(interactive "p")
(or arg (setq arg 1))
(if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
(signal 'end-of-buffer nil))
(if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
(signal 'beginning-of-buffer nil))
(cond ((zerop arg)
(save-excursion
(copy-region-as-kill
(progn (beginning-of-line) (point))
(progn (forward-visible-line 0) (point))))
(copy-region-as-kill
(progn (beginning-of-line) (point))
(progn (end-of-visible-line) (point))))
((< arg 0)
(save-excursion
(copy-region-as-kill (point) (progn (end-of-visible-line) (point))))
(copy-region-as-kill
(progn (end-of-line) (point))
(progn (forward-visible-line (1+ arg))
(unless (bobp) (backward-char))
(point))))
(t
(save-excursion
(copy-region-as-kill
(progn (beginning-of-line) (point))
(progn (forward-visible-line 0) (point))))
(copy-region-as-kill
(progn (beginning-of-line) (point))
(progn (forward-visible-line arg) (point))))))
Bind it to a key and use it like kill-whole-line
.
Maybe this package does what you want: https://github.com/skitov/linewise For copying multiple lines you need to select region that starts in whatever position in top line and ends in whatever position in last line, then use copy function of the package, then all lines will be in kill ring, lines will be always trailed with newline, so when you yank them it will keep lines separate from next line.