December 6, 2010

Integrating Emacs with the X11 Clipboard in Linux

The last post covered integrating Emacs with your Mac OS X clipboard. Integrating Emacs with the X11 clipboard in Linux is a little more work, but not much. The programming has already been done thanks to an add-on in the form of an .el file.

Linux users will need to do a little installing and downloading. First you'll need to install a program called xclip. Like pbcopy and pbpaste it works as a go-between for the X11 clipboard and console based applications. If you're on a Debian/Ubuntu based distro, just type sudo apt-get install xclip. On Fedora/Red Hat type su -c 'yum install xclip'. Be sure to include the single quotes.

Now we need to create a directory for your Emacs .el files. Emacs can be customized quite a bit, both by adding custom code to your .emacs file, and by using other add-ons that use the same programming language. These add-ons come in the form of .el files. First, let's make sure you don't already have an Emacs directory, type cd to switch to your home directory, then type ls -a and look for .emacs.d. If it's not there, then type mkdir .emacs.d to create it. Now download the xclip.el file, it's a text file, so you can right-click it and click Save as, make sure to include the .el extension, or go to the page and check out the code first if you prefer. Now switch back to the command line, type mv ~Downloads/xclip.el ~.emacs.d and replace the first directory with the directory you saved the xclip.el file in.

Finally we need to tell Emacs that the file is there. Type emacs .emacs to edit your configuration file. Add this line at the end of your file:
(load-file "/home/username/.emacs.d/xclip.el")

Save and exit Emacs. Next time you start up, you'll be able to copy and paste between Emacs and graphical X11 applications.

1 comment:

  1. In case you're using emacs inside of tmux or screen (so do I), you will need to manually set TERM variable to make xclip working:
    $ TERM=xterm emacs -nw some files