Pasting X selection (not clipboard contents) with keyboard

36,651

Solution 1

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)"'

Solution 2

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

Solution 3

xdotool click 2

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

Solution 4

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.

Solution 5

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

Share:
36,651

Related videos on Youtube

msaif
Author by

msaif

// shenanigans

Updated on September 18, 2022

Comments

  • msaif
    msaif over 1 year

    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?

  • msaif
    msaif about 13 years
    Definitely qualifies as "better". Thanks!
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 13 years
    In what application? None of the applications I use often behave like this.
  • Peter.O
    Peter.O about 13 years
    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 :(
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 13 years
    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.
  • msaif
    msaif almost 13 years
    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.
  • sup
    sup about 12 years
    xvkbd approach does not work well with non-English characters (like ěščřžýáíé) xdotool is fairly slow, delay 0 helps. However it seems to me that it has problems with newlines and characters like "&", "$" etc. I went for shift+insert, it seems to works surprisingly well.
  • tcoolspy
    tcoolspy almost 10 years
    @Luchostein Thanks for the suggested edit, sorry your actual revision got mistakenly rejected so you aren't properly credited but I did go back and implement the addition.
  • SomeUser
    SomeUser about 9 years
    I find an easy approach is to bind the keyboard shortcut to sending a middle click event. For me, that is xdotool click 2.
  • pfrenssen
    pfrenssen about 9 years
    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.
  • Ján Lalinský
    Ján Lalinský almost 6 years
    The xvkbd variant seems to work reliably for ASCII text on Ubuntu Desktop 16.04.4. The xdotool does not - the output is slow and some leading characters do not get typed, and the output is not consistent across serveral runs. I use a script that contains this: xsel | xvkbd -xsendevent -file - 2>/dev/null to suppress messages due to xvkbd. Non ASCII characters get mangled by both methods, unfortunately.