Pasting X selection (not clipboard contents) with keyboard

On some default linux setups, Shift+Insert will perform an X-selection-paste. As you noted, this is distinctly different from the X-clipboard-paste command, the binding for which often varies by application. If that doesn't work here are a couple other keys to try:

  • Ctrl+V

  • Ctrl+Shift+V

  • Ctrl+Shift+Insert

No go? Your Desktop Environment or Window Manager probably doesn't have them configured, and it's complicated because —even under the banner of one DE or WM— each toolkit (e.g. GTK, Qt, Etc.) may well have different default bindings. Some programs (e.g. gvim) even have their own internal copy registers that are not necessarily synced to the graphical environment they run in. To top it off, even when a program does use the X-clipboard system, X has multiple systems to choose from. The two most basic are the selection buffer —which always has whatever the last thing selected was (execpt when it doesn't)— and the copy buffer —which things usually need to be specifically copied into. To do an explicit copy into the latter system you can try any of these on for size:

  • Ctrl+C

  • Shift+Ctrl+C

  • Ctrl+Insert

If none of that is just magically working for you, there are two ways you can go.

  1. There's an app for that!™ Use one of the various clipboard manager programs to handle this for you. The most popular seem to be Parcellite and Glippy, but you can check out other alternatives here. See also this question about advanced clipboard managers

  2. Hack it yourself.

So lets say you want to hack it.

Short of writing your own code and tapping into the X api, the hacker tools for the job are a couple of little command line utilities that give you a window into the mind of X. Just a small window mind you, the whole view too scary.

The first tool is xsel. This little jobber will spit out whatever is in X's selection buffer at any given time.

Now you need to get that into your program. There are two options for this. One is xdotool which allows you to mimic sending events to the Xorg input system. You can use it's type method like xdotool type foo_bar to mimic typing 'foo_bar' at the cursor. Combined, you get something like this:

$ xdotool type $(xsel)

The other one is xvkbd which sends keyboard events from a lower subsystem. You can pipe keystrokes into it on STDIN. Combined with xsel, you get something like this:

$ xsel | xvkbd -xsendevent -file -

Great. Now for that keybinding to run this stuff. If you run Gnome-2, you can add a custom shortcut in System -> Preferences -> Keyboard shortcuts. If you use a different DE or WM this excersize is left up to the reader.

The last note is that when binding commands to keyboard shortcuts it is often necessary to only have one command, not two commands connected with a pipe like we use above. You can accomplish this by invoking your piped command as a command string argumetn to a new shell like this:

sh -c 'xsel | xvkbd -xsendevent -file -'
sh -c 'xdotool type "$(xsel)"'

Apparently Shift+Insert may not work properly on some installations of GTK 3, at least on FreeBSD. The issue is described as:

Shift-Insert is not pasting primary selection. Instead, it is bound to paste the clipboard (for which Control-V is already used). Hence, there is no keyboard-only way to insert primary selection. One must drag the mouse to there and middle click. This makes interaction between terminals and GTK uncomfortable.

A recent (as of this writing) bug report and patch are available:

xdotool click 2

This simulates the mouse button click directly, and does not require to use xsel / xdotool type ....