Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i paste the selected region outside of emacs?

Tags:

emacs

I am using Mac OS and emacs -nw (the terminal mode). I don't know how can I paste things (having been implemented by M-w in emacs -nw) outside the emacs.

I know that the emacs -ns can do it.

Searching the internet and the command C-h b, i find out that method, but it didn't work out.

(setq x-select-enable-clipboard t)

(setq interprogram-cut-function 'x-select-text)

I don't know much about the argument of interprogram-cut-function. Where does the x-select-text come from and what does it mean?

like image 371
luthur Avatar asked Mar 13 '11 08:03

luthur


People also ask

How do I copy and paste from outside in Emacs?

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

How do I copy a region 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 I paste from Emacs to clipboard?

Kill (Cut), Copy, and Yank (Paste) Commands in Emacs To cut, or kill, the text, you can use the keys Ctrl + k to kill a particular line, or the Ctrl + w command to kill the entire selected region. To paste, or yank, the text, press the keys Ctrl + y. This pastes the last killed item from the kill ring.


2 Answers

If you are using Ubuntu 12.04 or Fedora 21, there are a couple of options to make this work.

First you need to install xclip

sudo apt-get install xclip

First Option: For Emacs 24

If you are using emacs24 you can install from the list of packages

M-x package-list-packages

Select

xclip //mine was version 1.3

In your .emacs add:

(xclip-mode 1)

Second Option. For emacs before version 24

Install xclip.el: Integrating Emacs with the X11 Clipboard in Linux

Third Option. Using @Nicholas Riley code shown in the answer

To use the code in the answer you need pbcopy / pbpaste in Ubuntu (command line clipboard)

like image 81
elviejo79 Avatar answered Sep 18 '22 05:09

elviejo79


x-select-text is only used if you're running Emacs in a GUI. (Emacs maps the Mac/Windows pasteboard/clipboard APIs to the X11 model, hence the name). You can always use C-h f to find out more about a function like this one and view its definition if it's written in elisp.

On the Mac, there is no concept of CLIPBOARD versus PRIMARY selections, so there is no point in setting x-select-enable-clipboard.

The whole point of running emacs -nw is that it doesn't interact with the windowing system. Why use Emacs in a terminal when there are plenty of graphical Emacsen that work very nicely on the Mac?

That said, if you really wanted to hook up terminal Emacs to the Mac pasteboard, you could do something like this:

(setq interprogram-cut-function
      (lambda (text &optional push)
    (let* ((process-connection-type nil)
           (pbproxy (start-process "pbcopy" "pbcopy" "/usr/bin/pbcopy")))
      (process-send-string pbproxy text)
      (process-send-eof pbproxy))))
like image 36
Nicholas Riley Avatar answered Sep 22 '22 05:09

Nicholas Riley