Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy text from Emacs to another application on Linux

People also ask

How do I copy text in Emacs?

Once you have a region selected, the most basic commands are: To cut the text, press C-w . To copy the text, press M-w . To paste the text, press C-y .

How do you copy in Emacs terminal?

When you copy and paste text, the terminal does it : you're taking the text that the terminal displays and not the text that is in your emacs app. That's why you have to use terminal keybindings : Ctrl-Shift-C and Ctrl-Shift-V to copy and paste text.

How do I copy from Emacs to clipboard?

Using the copy function will place the selected text into the CLIPBOARD. Pasting using the middle mouse button will insert the PRIMARY selection. Pasting using the yank/paste functions will insert the CLIPBOARD. With this out of the way, starting with Emacs 24.1, GNU Emacs should already do the right thing here.

How do I copy and paste text in Linux?

Similarly, you can use Ctrl+shift+C to copy text from the terminal and then use it to paste in a text editor or web browser using the regular Ctrl+V shortcut. Basically, when you are interacting with the Linux terminal, you use the Ctrl+Shift+C/V for copy-pasting.


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 try

    META-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.


Insert the following into your .emacs file:

(setq x-select-enable-clipboard t)

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!


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)

I assume by emacs you are meaning Emacs under X (ie not inside a terminal window).

There are two ways:

  1. (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.
  2. 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

There is an EmacsWiki article that explains some issues with copy & pasting under X and how to configure it to work.