xsel: Copy & Paste from the shell

or how to abandon that pesky mouse.

Today I learned a very interesting command line tool.

Say you need to put something rapidly into the clipboard from the command line and output it somewhere else later.

I normally would’ve used the mouse for a quick copy paste.

But why use the mouse at all?

You don’t have to, the xsel [1] tool is here to help you.

See this StackOverflow answer [2] to the question “How do I send stdin to the clipboard?”.

I came across this tool when I was using gnu-screen [3] and after having created a new window I wanted to cd into the same directory in both windows.

To avoid getting out of the home row [4] I thought:

“Why not cd once into the path and then copy the output of pwd and pass it into cd in the other screen?”

So I did.

In window 1)

1
2
3
4
cd /path/to/folder
pwd | xsel -i -b

# -i means input and -b clipboard

Move to window 2)

1
2
3
cd $(xsel -o -b)

# $() executes the command within, -o is output

et voilà

Note that in this case there was no need to pass the -b parameter since I don’t really need it in the clipboard but my initial thought process involved having it there.

Another thing to note is that I couldn’t simply use environment variables [5] because they’re not shared between windows in gnu-screen.

Thoughts? Can you think of other useful use cases for this? Better ways to solve my dumb problem? gnu-screen/tmux/etc lovers/haters that want to contribute in any way?

[1] http://linux.die.net/man/1/xsel
[2] http://unix.stackexchange.com/questions/3892/how-do-i-send-stdin-to-the-clipboard
[3] http://unix.stackexchange.com/questions/549/tmux-vs-gnu-screen
[4] http://www.typing.com/tutor/
[5] https://wiki.archlinux.org/index.php/environment_variables