Go to content Go to navigation Go to search

Finally - Linux clipboard integration

One of the things that has always bothered me about working in a Linux environment is that the cut/copy/paste support is terrible. There are at least three different clipboards, none of which work well together. I always end up selecting some text, then trying to paste it into a window and getting the wrong clipboard. You have to deal with

  • the primary selection (what gets copied when you select text with the mouse)
  • the clipboard (what gets copied when you use Edit->Copy in Firefox, Eclipse, etc.)
  • the internal kill selection managed by the shell or editor – bash, zsh, or command line emacs for example.

I’ve also been annoyed that there is no easy way to copy from a terminal without using the mouse. The Mac ships with the nice pbpaste and pbcopy tools that allow you to copy from stdin and paste to stdout.

I finally discovered the tool that does what I want – and it’s at least six years old. xclip is a simple tool to copy from stdin, paste to stdout, copy from a file, and copy or paste from any of the various clipboards. For example, echo "copy this" | xclip" will set the primary selection to the echoed string. Middle click the mouse and it will be spit back out.

xclip is great on it’s own, especially for copying the output of a program for pasting into an email. But I wanted to go further than that. I wanted to be able to hit a key to copy the primary selection into the clipboard or vice versa. I use wmii as a window manager, so I set up a plug-in to bind MODKEY-x to copy the clipboard to the primary (X) selection, and MODKEY-c to copy the selection to the clipboard©:

#
# clipboard plugin
# by Derek Young
#
# Add key bindings to copy the primary selection to the clipboard,
# or clipboard to primary selection.
#
# sample config:
# # xclip settings
# use_binding "dmy999@gmail.com:primary-to-clip"
# use_binding "dmy999@gmail.com:clip-to-primary"
#
# Copyright Derek Young, 2007
# Use as you wish but please give credit.
#

Plugin.define "dmy999@gmail.com" do
  author '"Derek Young" <dmy999@gmail.com>'

  binding("primary-to-clip", "MODKEY-c") do |wmii,|
    LOGGER.info "copying primary selection to clipboard"
    system("xclip -o | xclip -sel clipboard")
  end
  binding("clip-to-primary", "MODKEY-x") do |wmii,|
    LOGGER.info "copying clipboard to primary selection"
    system("xclip -o -sel clipboard | xclip")
  end
end

Next, I wanted to integrate my new shell of choice, zsh, with the clipboard. I was about to start that work when I found that someone had already done the work: mouse.zsh by Stephane Chazelas. With this installed I can yank text from one terminal and paste it into another, or use my wmii key bindings to move the data around between clipboards.

At last – cut/copy/paste across everything I use.

  Textile Help