2

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?

  • possible duplicate of Kill or copy current line with minimal keystrokes – sanityinc Nov 16 '14 at 22:27
  • 1
    "How can I do copy in kill-whole-line way?" Kill then undo. – Malabarba Nov 16 '14 at 22:35
  • @sanityinc Uh? That question doesn't deal with copying multiple lines. – Gilles 'SO- stop being evil' Nov 17 '14 at 6:13
  • @Gilles Both questions are about how to copy a line without explicitly selecting it. – sanityinc Nov 17 '14 at 13:26
  • 1
    @sanityinc No, I'm asking how to define a function to copy multiple lines with optional prefix ARG without moving cursor. – CodyChan Nov 17 '14 at 17:10
2

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.

3

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)
0
(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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.