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

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

Re: Simple copy & paste problem


From: Pascal J. Bourguignon
Subject: Re: Simple copy & paste problem
Date: Wed, 08 Dec 2010 15:24:15 -0000
User-agent: Gnus/5.101 (Gnus v5.10.10) Emacs/23.2 (gnu/linux)

Andrea Crotti <andrea.crotti.0@gmail.com> writes:

> This is probably very stupid but every time I step into it and it's very
> annoying.
>
> Supposing I want to copy something from Safari to emacs.
> So I
> - select and copy
> - select and kill the text I want to substitute
> - try to paste
>
> ARGH! Now the text I copied from safari is disappeared...


> A workardound is to delete first and then copy-paste.

or:

- select and copy
- yank (it's called yank in emacs, not paste).
- select and kill the text you wanted to substitute


> But is there a smarter way to solve this?
> Something like
> "if I copied something from the os keep it as first in the kill ring"?

Use delete-region-and-yank instead of yank:

(defun delete-region-and-yank (&optional arg)
  "Deletes region if mark is active and yanks the last kill.
Always replaces the region with the yank, whether the region was
selected via keyboard or mouse.  Also works for normal
yank even with ARGS (thus it can be mapped to \\C-y)"
  (interactive "*P")                    ; raw, like yank.
  (message "arg=%S" arg)
  (cond
    (mark-active                        ; delete region
     (let ((str (buffer-substring (point) (mark))))
       (delete-region (point) (mark))
       (if (string=* str (current-kill 0 1))
           (let ((str2 (current-kill 1 1)))
             (kill-new str2 t))))
     (if arg
         (yank arg)
         (yank)))
    ;; else no region selected:
    ((consp arg)                        ; delete forward sexp
     (set-mark (point))
     (forward-sexp 1)
     (delete-region-and-yank))
    (arg (yank arg))
    (t   (yank))))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


reply via email to

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