When I cut (kill) text in Emacs 22.1.1 (in its own window on X, in KDE, on Kubuntu), I can't paste (yank) it in any other application.
-
1I suggest you read this: [CopyAndPaste-Emacs Wiki](https://www.emacswiki.org/emacs/CopyAndPaste). This explain very well tour problem. – Boris Valderrama May 25 '19 at 09:22
13 Answers
Let's be careful with our definitions here
- An Emacs copy is the command
kill-ring-save
(usually bound to M-w). - A system copy is what you typically get from pressing C-c (or choosing "Edit->Copy" in a application window).
- An X copy is "physically" highlighting text with the mouse cursor.
- An Emacs paste is the command
yank
(usually bound to C-y). - A system paste is what you typically get from pressing C-v (or choosing "Edit-Paste" in an application window).
- An X paste is pressing the "center mouse button" (simulated by pressing the left and right mouse buttons together).
In my case (on GNOME):
- Both Emacs and system copy usually work with X paste.
- X copy usually works with Emacs paste.
To make system copy work with Emacs paste and Emacs copy work with system paste, you need to add
(setq x-select-enable-clipboard t)
to your.emacs
. Or tryMETA-X set-variable RET x-select-enable-clipboard RET t
I think this is pretty standard modern Unix behavior.
It's also important to note (though you say you're using Emacs in a separate window) that when Emacs is running in a console, it is completely divorced from the system and X clipboards: cut and paste in that case is mediated by the terminal. For example, "Edit->Paste" in your terminal window should act exactly as if you typed the text from the clipboard into the Emacs buffer.

- 73,529
- 11
- 197
- 229

- 55,321
- 43
- 129
- 155
-
7[This is confusing terminology](http://unix.stackexchange.com/questions/158082/what-are-system-copy-and-paste). The standard, and less confusing name, for what you call “X copy/paste” is the X primary selection. What you call “system copy/paste” is the X clipboard. – Gilles 'SO- stop being evil' Sep 28 '14 at 22:04
-
Thanks... looks like the variable has been renamed since this was written. `META-X set-variable RET select-enable-clipboard RET t` or `(setq select-enable-clipboard t)` – James McCorrie Jun 13 '19 at 13:42
Insert the following into your .emacs
file:
(setq x-select-enable-clipboard t)

- 73,529
- 11
- 197
- 229

- 4,045
- 5
- 26
- 25
-
5
-
-
10Nice and simple, but this doesn't work in terminal. There's no reason why the same shortcut shouldn't work both in terminal and a free window, and some of the answers below address both simultaneously. – RussellStewart Jan 09 '14 at 20:59
-
2When I run `emacs -nw`, I use the terminal's own shortcuts; Shift-Ctrl-X to cut (kill) text, and Shift-Ctrl-C to copy. – memius Aug 04 '17 at 21:51
-
4That's not gonna work when you run `emacs -nw` inside horizontally split tmux windows. – Sridhar Ratnakumar Jun 20 '18 at 15:42
-
I'm running emacs -nw all the time, and ran into this issue. With -nw it's solved with xclip installed in both the os and emacs. c.f. https://stackoverflow.com/questions/5288213/how-can-i-paste-the-selected-region-outside-of-emacs – Boson Bear Jun 25 '20 at 06:35
-
-
Sadly for me this breaks cutting and deleting from a visual selection with vim in emacs. – RHS Jul 26 '21 at 15:41
-
@alper I don't know the answer to your question. I'm not familiar with macOS. – memius Sep 10 '21 at 14:36
-
@RHS I don't know the answer to your question. I'm not familiar with vim. – memius Sep 10 '21 at 14:37
-
`M-x` `customize-variable` `RET` `x-select-enable-clipboard` `RET` (and then toggle it and optionally save) is also a useful approach. – lindes Jan 16 '22 at 22:39
The difficulty with copy and paste in Emacs is that you want it to work independently from the internal kill/yank, and you want it to work both in terminal and the gui. There are existing robust solutions for either terminal or gui, but not both. After installing xsel (e.g. sudo apt-get install xsel
), here is what I do for copy and paste to combine them:
(defun copy-to-clipboard ()
(interactive)
(if (display-graphic-p)
(progn
(message "Yanked region to x-clipboard!")
(call-interactively 'clipboard-kill-ring-save)
)
(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!")))
)
(defun paste-from-clipboard ()
(interactive)
(if (display-graphic-p)
(progn
(clipboard-yank)
(message "graphics active")
)
(insert (shell-command-to-string "xsel -o -b"))
)
)
(global-set-key [f8] 'copy-to-clipboard)
(global-set-key [f9] 'paste-from-clipboard)

- 12,111
- 21
- 91
- 136

- 5,293
- 3
- 26
- 23
-
For I'm receiving error because there aren't params. I need to change the first line "(defun copy-to-clipboard ()" to "(defun copy-from-ubuntu (text &optional push)". This works for me :D – Renato Cassino Feb 18 '17 at 14:38
-
It works but paste operation is pretty slow, could it be realted to the network? – alper Jul 20 '20 at 11:47
-
Thanks a lot, it worked like a charm for me, using DoomEmacs in terminal (though without X support), on Ubuntu 20.04. – T. Rode Jun 08 '21 at 12:41
I stick this in my .emacs:
(setq x-select-enable-clipboard t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
I subsequently have basically no problems cutting and pasting back and forth from anything in Emacs to any other X11 or Gnome application.
Bonus: to get these things to happen in Emacs without having to reload your whole .emacs, do C-x C-e with the cursor just after the close paren of each of those expressions in the .emacs buffer.
Good luck!

- 8,751
- 24
- 32

- 36,964
- 10
- 32
- 35
-
5This solution works for emacs in an X Window i.e. emacs is running inside a window if you are running emacs in the command line what you need is: http://stackoverflow.com/questions/5288213/how-can-i-paste-the-selected-region-outside-of-emacs – elviejo79 Feb 02 '13 at 05:50
-
I assume by emacs you are meaning Emacs under X (ie not inside a terminal window).
There are two ways:
- (Applies to unix OS's only) Highlight the desired text with your mouse (this copies it to the X clipboard) and then middle click to paste.
- Highlight the desired text and then "M-x clipboard-kill-ring-save" (note you can bind this to an easier key). Then just "Edit->Paste" in your favorite app.
Clipboard operations available:
- clipboard-kill-ring-save -- copy selection from Emacs to clipboard
- clipboard-kill-region -- cut selection from Emacs to clipboard
- clipboard-yank -- paste from clipboard to Emacs

- 994
- 1
- 8
- 6
There is an EmacsWiki article that explains some issues with copy & pasting under X and how to configure it to work.
-
1There is an emacs wiki entry to copy and paste. That's how bad that is. – nicolas May 21 '20 at 16:46
This works with M-w
on Mac OSX. Just add to your .emacs file.
(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)

- 5,671
- 2
- 49
- 34
-
Ah I see, it also uses [this `interprogram-cut-function`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Low_002dLevel-Kill-Ring.html) builtin functionality which is meant to address this very question. – ijoseph Apr 11 '21 at 22:28
I use the following, based on the other answers here, to make C-x C-w
and C-x C-y
be copy and paste on both Mac and Linux (if someone knows the version for Windows feel free to add it). Note that on Linux you will have to install xsel and xclip with your package manager.
;; Commands to interact with the clipboard
(defun osx-copy (beg end)
(interactive "r")
(call-process-region beg end "pbcopy"))
(defun osx-paste ()
(interactive)
(if (region-active-p) (delete-region (region-beginning) (region-end)) nil)
(call-process "pbpaste" nil t nil))
(defun linux-copy (beg end)
(interactive "r")
(call-process-region beg end "xclip" nil nil nil "-selection" "c"))
(defun linux-paste ()
(interactive)
(if (region-active-p) (delete-region (region-beginning) (region-end)) nil)
(call-process "xsel" nil t nil "-b"))
(cond
((string-equal system-type "darwin") ; Mac OS X
(define-key global-map (kbd "C-x C-w") 'osx-copy)
(define-key global-map (kbd "C-x C-y") 'osx-paste))
((string-equal system-type "gnu/linux") ; linux
(define-key global-map (kbd "C-x C-w") 'linux-copy)
(define-key global-map (kbd "C-x C-y") 'linux-paste)))

- 86,894
- 26
- 169
- 240
-
2Actually, you can use `xclip` for pasting the clipboard content as well by using `xclip -o`. This can be useful for people that already have `xclip` and don't want to install another program to do both the copy and pasting. – Amin NAIRI Jun 19 '20 at 15:42
-
Note that this overwrites the default keybinding for `write-file` (C-x C-w). – Flux Dec 20 '20 at 00:00
The code below, inspired by @RussellStewart's answer above, adds support for x-PRIMARY and x-SECONDARY, replaces region-active-p
with use-region-p
to cover the case of an empty region, does not return silently if xsel has not been installed (returns an error message), and includes a "cut" function (emacs C-y, windows C-x).
(defun my-copy-to-xclipboard(arg)
(interactive "P")
(cond
((not (use-region-p))
(message "Nothing to yank to X-clipboard"))
((and (not (display-graphic-p))
(/= 0 (shell-command-on-region
(region-beginning) (region-end) "xsel -i -b")))
(error "Is program `xsel' installed?"))
(t
(when (display-graphic-p)
(call-interactively 'clipboard-kill-ring-save))
(message "Yanked region to X-clipboard")
(when arg
(kill-region (region-beginning) (region-end)))
(deactivate-mark))))
(defun my-cut-to-xclipboard()
(interactive)
(my-copy-to-xclipboard t))
(defun my-paste-from-xclipboard()
"Uses shell command `xsel -o' to paste from x-clipboard. With
one prefix arg, pastes from X-PRIMARY, and with two prefix args,
pastes from X-SECONDARY."
(interactive)
(if (display-graphic-p)
(clipboard-yank)
(let*
((opt (prefix-numeric-value current-prefix-arg))
(opt (cond
((= 1 opt) "b")
((= 4 opt) "p")
((= 16 opt) "s"))))
(insert (shell-command-to-string (concat "xsel -o -" opt))))))
(global-set-key (kbd "C-c C-w") 'my-cut-to-xclipboard)
(global-set-key (kbd "C-c M-w") 'my-copy-to-xclipboard)
(global-set-key (kbd "C-c C-y") 'my-paste-from-xclipboard)

- 463
- 6
- 11
-
Are there any reasons not to overwrite the standard keybindings? – Paul Brodersen Sep 07 '17 at 10:39
-
@Paul: The 'standard keybindings' are for the 'emacs internal clipboard', which is different from the X-clipboard. I quoted the term 'emacs internal clipboard' because the emacs terminology is different - refer to Chris Conway's excellent explanation, above. – user1404316 Sep 08 '17 at 13:18
Hmm, what platform and what version of emacs are you using? With GNU Emacs 22.1.1 on Windows Vista, it works fine for me.
If, by any chance, you are doing this from windows to linux through a RealVNC viewer, make sure you are running "vncconfig -iconic" on the linux box first.....

- 321
- 1
- 6
I always use quick paste -- drag selection in emacs, hit the middle mouse button in target window.
(From the reference to kate, I take it you're on linux or similar and probably using emacs in X one way or another.)

- 36
- 2
You might want to specify what platform you are using. Is it on linux, unix, macosx, windows, ms-dos?
I believe that for windows it should work. For MacOSX it will get added to the x-windows clipboard, which isn't the same thing as the macosx clipboard. For Linux, it depends on your flavour of window manager, but I believe that x-windows handles it in a nice way on most of them.
So, please specify.

- 19,783
- 6
- 37
- 57
What I do is to use a good terminal tool (PuTTY on Windows, Konsole or Terminal on Linux) that has copy facilities built-in.
In PuTTY, you highlight the text you want with the mouse and then paste it elsewhere. Right-clicking in a PuTTY window pastes the contents of the Windows copy/paste buffer.
In Konsole or Terminal on Linux, you highlight what you want then press Shift+Ctrl+C for copy and Shift+Ctrl+V for paste.
In the win32 compile of emacs, yanking text does put it on the copy/paste buffer .. most of the time.
On Mac OS X, the Apple-key chortcuts work fine, because Terminal traps them.
There is no direct way of doing it on the commandline because the shell does not maintain a copy/paste buffer for each application. bash does maintain a copy/paste buffer for itself, and, by default, emacs ^k/^y shortcuts work.

- 32,620
- 21
- 85
- 124