Summary: add anchor to the clipboard functions added from WishList for easier linking
C-vto copy and paste
DELdeletes the region; just typing replaces it.
M-wact on the current line when TransientMarkMode is not active
If you are having issues with cutting and pasting between emacs and other applications, it may be an issue with interactions between the UI’s clipboard or selection buffer and the emacs. Windows has a single clipboard buffer. X11 has more than one buffer. Consider these solutions:
(setq interprogram-cut-function 'own-clipboard) (setq interprogram-paste-function 'get-clipboard)
(setq x-select-enable-clipboard t)
See also http://www.oreillynet.com/lpt/wlg/6162 which suggests
(setq x-select-enable-clipboard t) ; as above (setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
This has recently been expanded with more possible customisations.
The following represents something extremely close in “feel” to other modern X11 desktop apps, but with “bonus” kill ring features. “feel”? that is to say, the precise key bindings are different to other X11 apps unless maybe you also turn on cua-mode, but the behaviour is very similar to C-x/C-c/C-v clipboard in other apps:
It is perhaps easier for some to learn that different keys doing similar things than the same keys doing different things, i.e. a newbie to emacs might analogise:
and with the following customizations, that analogy might actually work out okay:
; (transient-mark-mode 1) ; Now on by default: makes the region act quite like the text "highlight" in many apps. ; (setq shift-select-mode t) ; Now on by default: allows shifted cursor-keys to control the region. (setq mouse-drag-copy-region nil) ; stops selection with a mouse being immediately injected to the kill ring (setq x-select-enable-primary nil) ; stops killing/yanking interacting with primary X11 selection (setq x-select-enable-clipboard t) ; makes killing/yanking interact with clipboard X11 selection
;; these will probably be already set to these values, leave them that way if so! ; (setf interprogram-cut-function 'x-select-text) ; (setf interprogram-paste-function 'x-cut-buffer-or-selection-value)
; You need an emacs with bug #902 fixed for this to work properly. It has now been fixed in CVS HEAD. ; it makes "highlight/middlebutton" style (X11 primary selection based) copy-paste work as expected ; if you're used to other modern apps (that is to say, the mere act of highlighting doesn't ; overwrite the clipboard or alter the kill ring, but you can paste in merely highlighted ; text with the mouse if you want to) (setq select-active-regions t) ; active region sets primary X11 selection (global-set-key [mouse-2] 'mouse-yank-primary) ; make mouse middle-click only paste from primary X11 selection, not clipboard and kill ring.
;; with this, doing an M-y will also affect the X11 clipboard, making emacs act as a sort of clipboard history, at ;; least of text you've pasted into it in the first place. ; (setq yank-pop-change-selection t) ; makes rotating the kill ring change the X11 clipboard.
Not sure when this appeared in GNU emacs, or if it’s how the developers intended things, buts it’s useful for me (I habitually use emacs from CVS, BTW, so not sure when this tip first applies): The “x-select-enable-clipboard t” method seems to resuscitate the “traditional” (and IMHO deeply strange) emacs interaction with the X11 primary and clipboard selections. EVERYTHING you highlight as a region winds up in both PRIMARY and CLIPBOARD X11 selections. That is… painful. (but N.B. no longer true in CVS emacs, which has x-select-enable-primary to manage primary behaviour separately as above).
But, some heroic emacs developer saw fit to produce the following functions, though they are by default bound to X keys that I doubt many people have (actual keys called
cut/copy/paste), so one can put the following in
(global-set-key "\C-w" 'clipboard-kill-region) (global-set-key "\M-w" 'clipboard-kill-ring-save) (global-set-key "\C-y" 'clipboard-yank)
If you want to keep the C-w, M-w, and C-y original settings, there is another way is to bind these keys by using the default X11: – XinShi
(global-set-key [(shift delete)] 'clipboard-kill-region) (global-set-key [(control insert)] 'clipboard-kill-ring-save) (global-set-key [(shift insert)] 'clipboard-yank)
Another way to preserve the default C-w, M-w, and C-y behaviors is to assign one keystroke to an intelligent clipboard integration function. The following function either copies the current region to the system clipboard or, if no region is active, yanks the clipboard at point:
(defun clipboard-dwim () (interactive) (if (region-active-p) (clipboard-kill-ring-save (region-beginning) (region-end)) (clipboard-yank))) (global-set-key (kbd "C-c w") 'clipboard-dwim)
The emacs menu bar cut/copy/paste probably uses these too I think, but of course you’ve turned that off as you’ve been told that having the menu bar on will impede learning emacs. ☺
This produces behaviour I like: The current region becomes PRIMARY as one might expect (like most freedesktop.org compliant apps, so it can be directly middle-button pasted in X apps), but CLIPBOARD is NOT munged by merely highlighting a region (again like most freedesktop.org compliant apps, and unlike x-select-enable-clipboard) - Iff you kill and yank, it interacts with the X CLIPBOARD in a vaguely sane manner (as if
C-w/M-w/C-y were like
C-x/C-c/C-v in other X11 apps, roughly). This makes interacting with other X11 applications pleasant I think - you just learn that emacs uses different keys for cut/copy/paste, basically - though of course this behaviour might cause people to think the kill ring was a mere clipboard, which it isn’t, as a press of
M-y after a few kills will readily demonstrate ☺.
clipboard-yank in particular seems eerily smart.
Instead of relying on the above, you can install the xsel program for manipulating the X selection and pipe the region contents to it like this:
(defun yank-to-x-clipboard () (interactive) (if (region-active-p) (progn (shell-command-on-region (region-beginning) (region-end) "xsel -i -b") (message "Yanked region to clipboard!") (deactivate-mark)) (message "No region active; can't yank to clipboard!"))) (global-set-key [f8] 'yank-to-x-clipboard)
Delete ‘-b’ from the ‘xsel’ command above if you want the default behaviour of copying to the ‘Primary Selection’ rather than the ‘Clipboard Selection’.
A long time ago I used pc-selection-mode. Then I got a Kinesis keyboard (did not help my fingers) which made using shift and the arrow keys much harder. So I decided to switch to transient-mark-mode plus delete-selection-mode, and use f5 for mark, f6 for copy, f7 for paste, and f8 for delete.
Later I decided to get rid of delete-selection-mode, because getting out of an isearch (and stay) required me to type RET, and getting out of an active region required me to type C-g. Hitting C-g in isearch returns to the start of the search. And hitting RET with an active region replaced the text. Bad usability, here.
‘transient-mark-mode’is enabled and 3)
‘delete-selection-mode’is enabled, then if you search within the active region and end the search by
‘RET’(some or all of) the region is replaced by a newline.
‘RET’the search stops; that’s all. The buffer is not modified. I get this (good) behavior in
emacs -qwith either the standard
delsel.elor my own version (Lisp:delsel.el).
I completely agree with what Alex said. CUA-mode is too heavy handed, but it has some very nice non-cua related features. When Kim Strom refactors out the rectangle functionality, I’ll be a very happy man.
I’m now rolling my in my init file based on cua-lite-mode.
Incidently XEmacs has with very nice shift-select stuff built in.
TransientMarkMode usually only makes sense if your region is not usually active. You can deactivate the mark by using a command which uses the mark and does not change the buffer. I use M-w a lot to do that – even if I don’t really need the region in the kill-ring. If the region is inactive, the mark is still there and C-x C-x will still exchange point and mark and reactivate the region.
I have switched from PcSelectionMode to TransientMarkMode + DeleteSelectionMode because I stopped using the Shift+cursor keys to mark the region. I think that adds strain to your hands. But these days I use the default setup. I find that it works just as well. – AlexSchroeder
I have added the following lines to my .emacs file:
(require 'pc-select) (pc-selection-mode)
Now PC-like copy-and-paste works great, except when I try to copy to another application. To copy to another application, I now have to use the mouse. Is there any way for control-insert to mark a region as well as put it in the kill ring?
I am using GNU Emacs 20.7.1 on Linux.
interprogram-cut-function- you can use
M-x aproposto do this. Mine is set to copy to the X11 clipboard by default, I think, but then I don’t use pc-select. – CharlesSebold
interprogram-cut-functionshould be set to the string
"x-select-text", if I am not mistaken. Sorry I wasn’t being clear before; it is very strange to me that on my Emacs (running “vanilla,” -q -no-site-file), this variable defaulted to “x-select-text,” which is why I wasn’t being more specific. You are running this in X11, right? Or is this on the console? Ah-ha… bet that’s the problem. I’m out of my depth now; as far as I know there is no way to pass the region to the Linux console clipboard. Anybody else know about this…? – CharlesSebold
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
interprogram-cut-functionas well and also can’t seem to find what it should be set to. The defun
x-select-textdoesn’t exist in XEmacs. – LathI
(setq interprogram-cut-function 'own-clipboard) (setq interprogram-paste-function 'get-clipboard)
In Emacs 21.2.1 and newer you can simply do
(setq x-select-enable-clipboard t) to make cutting and pasting use the X clipboard.
I am sometimes using several Emacsen in a screen session. Does someone know a way to do cutnpaste from one of those Emacsen to another? I thought about a shared kill-ring in a file in the homedirectory. Does something existing this direction?
This page should also address the problems with copy/paste and Unicode. Currently it seems that no released version of emacs or xemacs supports UTF8_STRING selections, so pasting text from browsers with UTF-8 hyphens, “smart quotes”, or other non-ASCII characters yields a mess. See e.g. http://mail.nl.linux.org/linux-utf8/2003-04/msg00060.html and http://lists.freedesktop.org/archives/xdg/2003-December/003081.html
See the spec in http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/
It seems that this is fixed in the yet-to-be-released emacs 22, and probably in xemacs 21.5 (?). In the meantime, I’d love to at least see a utility applet to convert UTF8_STRING selections to the closest Latin-1 equivalent --NealMcBurnett
One of the drawbacks of integrating the GNU Emacs copy-and-paste mechanism with X is that Emacs will grab the clipboard or selection every time you do a kill. This becomes a problem over slow network links (such as, for example, a DSL connection from home to work, and from there to a remote datacenter). I wrote replacements for myself that make clipboard operations explicit. This works with CVS GNU Emacs. Note that this affects the clipboard, so it will work with Firefox and “modern” X apps, but older X apps like xterm need to be modified to use CLIPBOARD rather than the PRIMARY selection.
;; Disable automatic cutting and pasting to the clipboard. ;; This causes noticeable delays over slow network links. ;; The function `insert-clipboard-contents' and the function ;; `set-clipboard-contents' let me do this explicitly. (setq interprogram-cut-function nil) (setq interprogram-paste-function nil)
(defun get-clipboard-contents-as-string () "Return the value of the clipboard contents as a string." (let ((x-select-enable-clipboard t)) (or (x-cut-buffer-or-selection-value) x-last-selected-text-clipboard)))
(defun insert-clipboard-contents () "Insert the value of the current X selection at point. Uses the clipboard value if it is defined or not empty, otherwise falls back on the primary selection." (interactive) (let ((text (get-clipboard-contents-as-string))) (when text ;; This operation is very much like a yank, so set mark like ;; yank does. Note that the "longlines" mode advice on this ;; function depends on mark having been set. (push-mark) (insert text))))
(defun set-clipboard-contents-from-string (str) "Copy the value of string STR into the clipboard." (let ((x-select-enable-clipboard t)) (x-select-text str)))
(defun set-clipboard-contents (beg end) "Copy the value of the current region into the clipboard." (interactive "r") (set-clipboard-contents-from-string (buffer-substring-no-properties beg end)) (setq deactivate-mark t))
(defun set-clipboard-contents-delete (beg end) "Cut the value of the current region into the clipboard. The current region is deleted (without updating the kill ring)." (interactive "r") (set-clipboard-contents-from-string (buffer-substring-no-properties beg end)) (delete-region beg end) (setq deactivate-mark t))
(defun set-clipboard-contents-and-kill-ring-from-string (str) "Copy the value of string STR into the clipboard, and make it the latest kill." (set-clipboard-contents-from-string str) (kill-new str) (message "%s" str))
(eval-after-load "longlines" '(progn (defadvice insert-clipboard-contents (after longlines-decode-kill activate) ;; Depends on insert-clipboard-contents having set mark at the ;; begging of the text. If we have to stop doing that, we ;; should turn this into "around" advice that can capture point ;; before doing the insert. (when longlines-mode (longlines-decode-region (point) (mark t)) (when longlines-showing (longlines-show-hard-newlines)))) (defadvice set-clipboard-contents (around longlines-encode-kill activate) (if longlines-mode (let ((str (buffer-substring beg end))) (with-temp-buffer (insert str) (longlines-encode-region (point-min) (point-max)) (setq beg (point-min)) (setq end (point-max)) ad-do-it)) ad-do-it)) (defadvice set-clipboard-contents-delete (before longlines-encode-kill activate) (when longlines-mode (longlines-encode-region beg end)))))
(defun path-to-clipboard () "Copy the current file's path to the clipboard.
If the current buffer has no file, copy the buffer's default directory." (interactive) (let ((path (expand-file-name (or (buffer-file-name) default-directory)))) (set-clipboard-contents-from-string path) (message "%s" path)))
(defun npath-to-clipboard () "Copy the current file's path to the clipboard, with a network filename. The resulting value will have the correct syntax to use with SCP.
If the current buffer has no file, copy the buffer's default directory." (interactive) (let* ((host (system-name)) (path (expand-file-name (or (buffer-file-name) default-directory))) (network-path (concat host ": class="string">" path))) (set-clipboard-contents-from-string network-path) (message "%s" network-path)))
Some of these functions then get bound to keys. Rather than going with a full CUA interface, I free up Control-Z as a new prefix.
;; Turn Control-z into another keymap. The previous binding becomes ;; `Control-z Control-z'. This frees up some easy-to-reach ;; keybindings for my own use. (defvar ctrl-z-map (make-sparse-keymap)) (let ((orig-ctrl-z-binding (lookup-key (current-global-map) [(control ?z)]))) (global-set-key [(control ?z)] ctrl-z-map) (global-set-key [(control ?z) (control ?z)] orig-ctrl-z-binding)) (global-set-key [(control ?z) (control ?g)] 'keyboard-quit)
;; Control-Z has been converted to be a new keymap, like Control-X. ;; This is a backup for when the mouse's rocker switch isn't working ;; (such as in Exceed). (global-set-key [(control ?z) (control ?x)] 'set-clipboard-contents-delete) (global-set-key [(control ?z) (control ?c)] 'set-clipboard-contents) (global-set-key [(control ?z) (control ?v)] 'insert-clipboard-contents)
;; Buttons 6 and 7 on my mouse are a rocker switch; forward is 6, ;; backward is 7. When I push forward, I'm "pushing the clipboard ;; contents into Emacs". When I pull backward, I'm "pulling from ;; Emacs into the clipboard". (global-set-key [(mouse-6)] 'insert-clipboard-contents) (global-set-key [(mouse-7)] 'set-clipboard-contents)
I’ve been trying off and on for several months to be able to copy and paste text between X and Emacs. I’m using Emacs 22.1.1 and KDE 3.5.8 and I run Emacs inside a Konsole window. I have the following in my .emacs:
(setq x-select-enable-clipboard t) (global-set-key [(shift delete)] 'clipboard-kill-region) (global-set-key [(control insert)] 'clipboard-kill-ring-save) (global-set-key [(shift insert)] 'clipboard-yank)
I also have delete-selection-mode enabled. And I’ve tried setting various values for interprogram-paste-function and interprogram-cut-function most of which tend to stop even normal yanking and killing from working! I’ve tried with and without Klipper (KDE’s clipboard management tool).
The result is that I can’t move text from the Emacs kill ring (or an Emacs selection) into my X11 clipboard. I can paste from the X11 clipboard into Emacs using Konsole’s paste function (though this has the effect that, from Emacs’ point of view, the text was typed). The only thing I can do is use Konsole’s copy function. However this has some serious drawbacks: it requires that I select text with the mouse; it doesn’t handle correctly lines which Emacs has wrapped (it interprets them as being multiple lines ending with “/“s); and of course you can’t select more text from a buffer than you can fit in a window.
I wonder whether there is a solution to this?
If Emacs is running inside of Konsole, then it doesn’t consider itself to be running in X. It’s running in a terminal. None of the X clipboard functions are going to to anything. What are working for you in your experiments are the Konsole bindings. You need to get Emacs to display its frame outside of Konsole.
I use gnome-terminal with Emacs with xt-mouse.el. To get text from X11’s clipboard into Emacs, I use shift-middle click and to get text out of Emacs, shift-select some text and then middle click to paste in some other app. In gnome-terminal holding shift bypasses any program’s that run in the terminal that use the mouse (like xt-mouse.el), this probably works in Konsole, too.
Guess with the most recent CVS emacs enabling cua-mode is enough. Anything else causes problems.
I’m a fan of independent CLIPBOARD (accessed via explicit edit commands like copy and paste) and PRIMARY (accessed via mouse marking and middle mouse click) selections and have a setup as advised here:
CLIPBOARD is always in sync with the head of the kill ring, that is, yank will either insert kill ring’s top entry or an external copy, whichever is more recent. Cutting or copying will also export this element as CLIPBOARD to other programs. S-insert and middle mouse button are used for pasting the PRIMARY selection, created via mouse selection.
(custom-set-variables '(mouse-sel-mode t) )
(defun insert-from-clipboard () "Insert the text from the current x-selection." (interactive) (insert (x-selection-value 'PRIMARY))) (global-set-key [(S-insert)] 'insert-from-clipboard)
;; export last insert into kill-ring as PRIMARY selection to other apps (defun my-x-select-text (text &optional push) (let ((x-select-enable-clipboard t) (x-select-enable-primary nil)) (x-select-text text push))) (setq interprogram-cut-function 'my-x-select-text)
; We have to be very carefull never to return the same thing twice, as ; it will be put into the kill-ring. ; If we want to express (car kill-ring), just return nil (defun my-x-yank () (interactive) (let ((ck (car kill-ring)) (xgsc (x-selection-value 'CLIPBOARD))) (if (not (string= ck xgsc)) xgsc nil ;; caller uses (car kill-ring) ))) (when (string= (window-system) "x") (setq interprogram-paste-function 'my-x-yank))
1. Link Emacs to OS X
(defun copy-from-osx () (shell-command-to-string "pbpaste")) (defun paste-to-osx (text &optional push) (let ((process-connection-type nil)) (let ((proc (start-process "pbcopy" "*Messages*" "pbcopy"))) (process-send-string proc text) (process-send-eof proc)))) (setq interprogram-cut-function 'paste-to-osx) (setq interprogram-paste-function 'copy-from-osx)
2. Link tmux to OS X by setting
reattach-to-user-namespace as the tmux default command as explained at: https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard
Clipx uses C-v to insert clipboard contents, so if you don’t use standard Emacs keys for scrolling then you can remap C-v to
‘yank’ and this makes Clipx interaction seamless:
(global-set-key (kbd "C-v") 'yank)
Emacs 23.1 joined the killring and the system pasteboard. I prefer the old behavior, when they were separate. This will achieve just that (tested on OS X):
;; (setq interprogram-cut-function 'x-select-text) ;; (setq interprogram-paste-function x-cut-buffer-or-selection-value) (setq interprogram-cut-function nil) (setq interprogram-paste-function nil) (defun paste-from-pasteboard () (interactive) (and mark-active (filter-buffer-substring (region-beginning) (region-end) t)) (insert (ns-get-pasteboard)) ) (defun copy-to-pasteboard (p1 p2) (interactive "r*") (ns-set-pasteboard (buffer-substring p1 p2)) (message "Copied selection to pasteboard") ) (defun cut-to-pasteboard (p1 p2) (interactive "r*") (ns-set-pasteboard (filter-buffer-substring p1 p2 t)) ) (global-set-key (kbd "s-v") 'paste-from-pasteboard) (global-set-key (kbd "s-c") 'copy-to-pasteboard) (global-set-key (kbd "s-x") 'cut-to-pasteboard)
Brought this from the WishList page.
(defun copy (beg end) "Copy region into clipboard" (interactive "r") (if mark-active (progn (x-set-selection 'CLIPBOARD (buffer-substring beg end)) (setq mark-active nil) (message "Marked text copied")) (progn (x-set-selection 'CLIPBOARD (buffer-substring line-beginning-position line-end-position)) (setq mark-active nil) (message "Current line is copied")))) (defun cut (beg end) "Copy region into clipboard and kill" (interactive "r") (if mark-active (progn (x-set-selection 'CLIPBOARD (buffer-substring beg end)) (delete-region beg end) (setq mark-active nil) (message "Marked text cut")) (progn (x-set-selection 'CLIPBOARD (buffer-substring line-beginning-position line-end-position)) (delete-region line-beginning-position line-end-position) (setq mark-active nil) (message "Current line is cut")))) (defun paste (beg end) "Paste contents of clipboard into point" (interactive "r") (if mark-active (delete-region beg end)) (insert (x-selection 'CLIPBOARD)))
But i think what it very useful function for everyone.
VolatileHighlightsMode brings visual feedback of “yank” and “yank-pop” operations by making highlights on text inserted by these commands.
I like having my emacs and X clipboards synced as much as possible. I don’t use any sort of DE clipboard manager, but I wanted to be able to put every X “clipboard” selection onto the emacs kill-ring. This is a script that can be run in background to do this.
#!/bin/bash POLL_TIME=1 STRING=first while : do CUR=`xclip -selection c -o` if [ "$CUR" != "$STRING" ]; then STRING=$CUR emacsclient -e "(kill-new \"$STRING\")" > /dev/null fi sleep $POLL_TIME done