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

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

Re: Copy and Pasting in Emacs...


From: Stein A. Stromme
Subject: Re: Copy and Pasting in Emacs...
Date: Mon, 08 Mar 2004 15:00:57 +0100
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3.50 (gnu/linux)

[Stefan Monnier on copying a line]

|        Stefan "who'd use C-k C-y to copy and then C-y to yank"

I have this in my .emacs, stolen from Xemacs but works in GNU Emacs
as well:

    ;;; Utility from xemacs/lisp/misc.el

    (defun copy-from-above-command (&optional arg)
      "Copy characters from previous nonblank line, starting just above point.
    Copy ARG characters, but not past the end of that line.
    If no argument given, copy the entire rest of the line.
    The characters copied are inserted in the buffer before point."
      (interactive "P")
      (let ((cc (current-column))
        n
        (string ""))
        (save-excursion
          (beginning-of-line)
          (backward-char 1)
          (skip-chars-backward "\ \t\n")
          (move-to-column cc)
          ;; Default is enough to copy the whole rest of the line.
          (setq n (if arg (prefix-numeric-value arg) (point-max)))
          ;; If current column winds up in middle of a tab,
          ;; copy appropriate number of "virtual" space chars.
          (if (< cc (current-column))
          (if (eq (char-before (point)) ?\t)
              (progn
            (setq string (make-string (min n (- (current-column) cc)) ?\ ))
            (setq n (- n (min n (- (current-column) cc)))))
            ;; In middle of ctl char => copy that whole char.
            (backward-char 1)))
          (setq string (concat string
                   (buffer-substring
                    (point)
                    (min (save-excursion (end-of-line) (point))
                     (+ n (point)))))))
        (insert string)))

    (define-key global-map [(insert)] 'copy-from-above-command)

So to copy a line I move point below the line and do C-o (if
necessary, to make space for the new line) and then [insert].
(Feel free to use a different keystroke if you actually need the
insert key for something else.)

Of course this is only useful if you really want the new copy to come
immediately after the original, but I find this often to be the case.

SA
-- 
Stein Arild Strømme            +47 55584825, +47 95801887
Universitetet i Bergen                  Fax: +47 55589672     
Matematisk institutt               www.mi.uib.no/stromme/         
Johs Brunsg 12, N-5008 BERGEN           stromme@mi.uib.no


reply via email to

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