How can I copy from emacs and paste into another instance of emacs or else where?

If I do C-space, M-w, and then C-y, I can only paste in the open instance of Emacs. If I navigate to another Emacs window or outside of Emacs, Crtl + v does nothing or paste something I copied that wasn't in Emacs.

I am looking for a solution the updates the init lisp script (~/.emacs). I don't want to switch programs. As of now, I have this in my .emacs but it doesn't seem to do the trick.

;; Copy and paste between Emac instances                                          
(setq x-select-enable-clipboard t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)

If you aren't running emacs in X mode, then it doesn't have access to the X clipboard, so you can't copy/paste with other X apps. You will have to use the copy/paste function of your xterm instead.

To copy/paste between different emacs sessions, the emacs way is to not run multiple sessions. Instead you can open frames on different terminals that are all bound to the same session, so they share the same kill ring. You do this by using M-x server-start in the main instance, then use emacsclient -t to open a new frame in another terminal.

  • can I set the server start in the ~/.eacs?
    – dustin
    Apr 16 '13 at 14:28
  • @dustin, yes....
    – psusi
    Apr 16 '13 at 14:31
  • Do you know the lisp command for it?
    – dustin
    Apr 16 '13 at 14:35
  • 2
    @dustin, (server-start) of course.
    – psusi
    Apr 16 '13 at 14:38
  • this may be a long shot but is there a way to setup an alias for emacs ~/path/to/file so it is recognized as emacsclient - t? Maybe something like alias emacs='emacsclient -t' but then that would override my alias emacs='emacs -nw'
    – dustin
    Apr 16 '13 at 14:44

I'm not an emacs user, but if you work outside of a GUI environment you can use the general purpose mouse (gpm) daemon on linux and I would guess some other *nixes. Most distros have it available as a package, so all you need to do is install it and start the service.

You can cut and paste in a VC this way with a mouse (I think button 1 is select, button 2 is paste -- there is no popup menu; this takes a bit of getting used to). You can, in fact, copy and paste across VC's. It works with complex ncurses apps akin to emacs such as vim.

  • My take is that 99% of the "outside of a GUI" people will not wish to use the mouse. Apr 16 '13 at 23:04
  • There is no other option if you want to copy/paste between applications, because without gpm there is no buffer to use. Judging from the OP's selected answer though, what s/he really wanted was to just share selections between emacs instances.
    – goldilocks
    Apr 17 '13 at 10:45
  • Check out this question on GNU readline, and especially the answer by @jordanm - what we need is a global kill ring, for all the Linux VTs; for xterm, urxvt, etc., launched in X; and for Emacs. Apr 18 '13 at 14:42
  • by using gpm, if a copy operation is made within the emacs could it also be seen from gpm? @goldilocks
    – alper
    Jun 13 '20 at 23:30
  • I would guess that if you use the mouse to do it yes, presuming it doesn't involve emac's clipboard buffers (meaning maybe not so useful except across instances).
    – goldilocks
    Jun 15 '20 at 13:41

You can use the Edit tab to use the mouse for it (at least in XEmacs, haven't used GNU Emaccs in quite a while).

  • 1
    I do all my programming and tex from the terminal so that won't work for me.
    – dustin
    Apr 16 '13 at 11:48
  • Use X... I'm no GUI guy, it is just a convenient way of having a lot of terminals around, and have a preview on LaTeX nearby..
    – vonbrand
    Apr 16 '13 at 11:57
  • 1
    I don't plan on switching I am looking for a solution for emacs.
    – dustin
    Apr 16 '13 at 13:41
  • No, you don't want to use the mouse, and it is not the Emacs kill ring anyway. Apr 16 '13 at 23:07

To get access to the actual Emacs kill ring, I don't know, but if you find out, please tell us, and I'd be very appreciative. But there are some other things I know of that might help you.

It is not true that you can't access the X clipboard in the Linux VT/tty/console.

Check out those aliases, as they are shown when I use where in zsh:

xi: aliased to xclip -d ":0" -selection clipboard
xo: aliased to xclip -d ":0" -selection clipboard -o

I also put the xo part into a script (called pst), so as to paste from X to Emacs.


CLIP=`xclip -d ":0" -o -selection clipboard`
CLIP_LEN=`echo -n $CLIP | wc -c`
echo "(set-variable '*clip-len* $CLIP_LEN)" > ~/.clip_len
echo -n $CLIP

Then, in Emacs:

;; clipboard
(setq *clip-len* 0)

(defun pst ()
  "Inserts the X clipboard (xclip -d ':0' -o -selection clipboard)
at point. `M-1' (the '(1)) to insert in the current buffer."
  (shell-command "/home/user_name/scripts/pst" '(1))
  (load-file "/home/user_name/.clip_len")
  (forward-char *clip-len*) )

Now, to get Emacs-Emacs communication (and I agree several instances is not optimal), if they share access to a file, how about using it by setting up functions like:

(defun write-to-file-clipboard ()
  "Write the region to `~/.fclipboard.txt'"
  (write-region (region-beginning) (region-end) "~/.fclipboard.txt") )

Then, do a similar for append-to-file, insert-file, etc., and then setup shortcuts and/or (Emacs) aliases for fast access.

This is of course not the Emacs kill ring but at least I found them very useful as a "next best" thing.


This question has been answered here. The linked answer includes a discussion (above), and a few code snippets to allow interoperability between the emacs clipboard and the GUI, X clipboard, even when running emacs-nox or emacs -nw.

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.