100

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?

1

5 Answers 5

108

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)"'
10
  • 1
    Definitely qualifies as "better". Thanks!
    – intuited
    Commented Apr 23, 2011 at 20:05
  • In what application? None of the applications I use often behave like this. Commented Apr 24, 2011 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
    Commented Apr 25, 2011 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. Commented Apr 25, 2011 at 17:41
  • 2
    I find an easy approach is to bind the keyboard shortcut to sending a middle click event. For me, that is xdotool click 2.
    – diwhyyyyy
    Commented Apr 13, 2015 at 11:49
11

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

0
8
xdotool click 2

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

1
  • 2
    This requires positioning the mouse properly, so you might as well actually use the mouse button. If there were some way to snap the mouse to the cursor this might be part of an answer though (I doubt there is)
    – jberryman
    Commented Jun 26, 2019 at 15:47
2

I am using Ubuntu 12 and was having a problem pasting text from xterm in gedit (and any other app). Also, I use a laptop with no middle mouse button like many people (there really is no clipboard mercy for linux users without middle buttons, and no: right-click+left_click is not working as a substitute).

While I think the "xsel | xvkbd -xsendevent -file -" method is pretty cool. I found the simplest solution was to run the preinstalled "gnome-terminal" instead of "xterminal". "gnome-terminal" supports a right-click copy to clip-board command. I could then "Edit Menu->Paste" into gedit or use shift-insert. "gnome-terminal" came preinstalled with Ubuntu 12. Other debian/gnome installations should be able to install it via:

sudo apt-get install gnome-terminal

In Ubuntu, add it to the left launcher by clicking "Dash Home" button at the top of the launcher bar. In the resulting search box, type terminal. You can drag the "Terminal" icon to the launcher bar.

1
  • 4
    This is a very poor solution. You cannot really suggest people to switch to a different terminal, especially one that will pull in 100MB+ of gnome-shell as a dependency. This also doesn't make it work with any other application, and worst of all this still requires the use of a mouse to work.
    – pfrenssen
    Commented Apr 29, 2015 at 12:42
2

On my 20.04 Alt+Shift+Insert paste the same as middle click

1
  • works for gnome-terminal, but not for e.g. Chrome :/ Commented Jul 3, 2023 at 15:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .