Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to be able to paste the X selection using the keyboard. Currently I have to use the middle mouse button to do this.

I gather that faking a middle mouse button press is fairly easy to do, but such a solution would also require moving the mouse pointer to the location of the text caret.

Is there a better way to do this?

share|improve this question

2 Answers 2

up vote 33 down vote accepted

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)"'
share|improve this answer
    
Definitely qualifies as "better". Thanks! –  intuited Apr 23 '11 at 20:05
    
In what application? None of the applications I use often behave like this. –  Gilles Apr 24 '11 at 15:01
    
I'm using Ubuntu, and I'd really like Shift-Insert to be of any practical use, but it simply isn't (in Ubuntu).. The only "consistancy (+/-)" I've found is that an X-selection in GTK apps can be Shift-Inserted into a gnome-terminal, but it is quite hit-and-miss for any other cross-pasting combination of these same apps... The few KDE apps I use (including Konsole), simply don't cross-paste at all.... and even in the GTK apps, centre-click may work, but Shift+Insert may not (for the same app)... So I use the mouse :( –  Peter.O Apr 25 '11 at 15:35
    
There's no such thing as an “X level binding”. It could be something the applications you use do, or something your window manager or desktop environment does. I doubt it would be specific to a distribution. –  Gilles Apr 25 '11 at 17:41
    
When I first tried Shift-Insert, it worked, and I jumped to the conclusion that it "just worked" for me. It looks like it works in roxterm, or possibly bash itself (under ubuntu), but not in general. I don't recall problems with KDE apps, but I don't really use them these days. Thanks for the addition of the DIY method, I may try using that. –  intuited Jun 30 '11 at 11:42

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:

http://www.freebsd.org/cgi/query-pr.cgi?pr=188264

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.