Keyboard shortcut for pasting the primary selection
Solution 1
Sending virtual keypresses doesn't work for me (see comments), but that answer inspired me to look for similar solutions. Sending a "text" event with xvkbd
ignores the current state of your physical keyboard:
sh -c 'xsel | xvkbd -xsendevent -file - 2>/dev/null'
xvkbd -text
uses a few backslash sequences, so rather than dance with escaping, -file
works. Add -delay 0
to enter the text without delay between the “keystrokes”. xvkbd
also outputs some warning text about modifiers, but it appears to be irrelevant to this use (but I didn't want to see it in ~/.xsession-errors
).
I bound this to a shortcut using System > Preferences > Keyboard Shortcuts.
Note that you need to have xsel and xvkbd packages installed:
sudo apt-get install xsel xvkbd
Solution 2
I was looking for an answer for this very same question, and I found this answer that says that Shift+Insert is working to paste the primary selection. I works for me. Simpler.
Solution 3
You can get this with the combined use of the programs xdotool (click to install) and xsel (click to install).
xdotool
can simulate typing into a window; xsel
outputs the
contents of the PRIMARY selection (by default); the following shell
one liner will do the trick:
xdotool type `xsel`
To bind this to any key using the System->Preferences->Keyboard shortcuts menu item it is necessary to wrap it in a shell invocation:
sh -c 'xdotool type --clearmodifiers -- "`xsel`"'
Typing in xdotool
will not work with some programs; see the notes in
the xdotool documentation.
Solution 4
Calling xdotool click --clearmodifiers 2
simulates clicking middle mouse button. This works much better than using xsel (at least for me). Altough you have to position your mouse before typing...
Solution 5
Another xdotool suggestion, working in Debian Jessie 8.7 (Jan 2017):
xdotool click --delay 0 --clearmodifiers 2
- xdotool handles multi-byte strings (p.e. UTF-8), unlike xvkbd.
xdotool click
simulates an actual click, so you don't have to click yourself to paste at mouse position, as you would have if you usedxdotool type
, or xvkbd.
The only problem is that --clearmodifiers
will "press" back any modifier (Ctrl/Alt/Shift/Meta) you use after simulating the click. Even with --delay 0
(instead of 12ms), the command takes a little to execute. If you release the keys before it ends, your modifiers will be "pressed" again, and stuck there until you press and release the actual key once more.
So with this you have to be a bit "slow" (50ms?) to release your modifiers, if you're using any.
You can test this by setting up your keyboard shortcut, using it into a text editor, and then pressing a key (like a
, or an arrow). The letter should appear in lowercase. If something else happens, you are too fast and a modifier is stuck (p.e. Shift if it's in caps, Ctrl if you selected all text, Alt if you opened a menu). Press and release your modifiers again to reset them.
If you're too fast, you can use ilkerk's suggestion:
sh -c 'sleep 0.3 && xdotool type --clearmodifiers --delay 0 "`xsel`"'
Then you have to release them in less than 300ms, and wait half a second for the text to appear.
Also, using xdotool type
means you insert the text as if you where typing, at the text cursor position, and not the mouse pointer. You can change it to click
if you prefer the usual middle-click behaviour.
(made this post so newcomers don't have to piece the puzzle pieces spread in the comments again)
Related videos on Youtube
Comments
-
flo over 1 year
Is there a standard or conventional keyboard shortcut for pasting the primary selection?
I'd like to select some text and go to another app to paste without trashing my clipboard contents. I'd rather keep my fingers on the keyboard than move to the mouse, find the pointer, position it where I want, and middle-click.
If there's nothing conventional, or if it's application-specific and unsupported by many, is there a workaround to get this working globally? (Or at least in more applications.)
-
Ciro Santilli OurBigBook.com about 9 yearsPossible same any distro: unix.stackexchange.com/questions/11889/…
-
Jonathan Hartley almost 7 yearsFWIW, you can paste the primary selection from the keyboard (ie. without using mouse buttons) using shift-insert, and copy / paste the clipboard selection using ctrl-insert / ctrl-shift-insert
-
jberryman almost 5 years@JonathanHartley for me shift-insert pastes from the clipboard not primary selection
-
-
Computer User over 13 yearsThat won't work unless the active text box and cursor position is at that exact position, and it certainly isn't. In fact, if this counts the window manager border, that will do my "middle-click on window title" action, which is completely different.
-
Riccardo Murri over 13 years@Roger Thanks, corrected the text. I'm curious whether there's a better way to do this: I myself have been looking for a "paste"-key for quite some time...
-
Computer User over 13 yearsWell, I appreciate the effort, but this still won't work. If I wanted to worry about the mouse position, I'd just use the mouse in the first place. :)
-
Riccardo Murri over 13 years@Roger: Got it correct second time round: xdotool+xsel does it. (Well, at least works for me.)
-
Computer User over 13 yearsNeeds to be
"$(xsel)"
to allow for spaces, but otherwise this seems to work perfectly! -
Computer User over 13 yearsNeeds --clearmodifiers option, otherwise there's a race condition between releasing the keyboard shortcut and xdotool executing. The xdotool in 10.04's repos isn't updated for this option, I had to download and build it myself. However, --clearmodifiers seems to strangely affect the modifiers, and I don't see how, in the end, to make xdotool work for my keyboard shortcut (though it works great for virtually typing text).
-
Computer User over 13 yearsIn case it's not clear from my comment: I was testing with super(winkey)-v; without --clearmodifiers, super is still held down when xdotool types, and since I use that modifier key for most of my window management actions...
-
Riccardo Murri over 13 yearsOh, I see: I was using key "F9" for my tests...
-
MK. over 13 yearsHoly bananas! It works! Thats awesome. Ubuntu should set this up by default.
-
sup about 12 yearsFor me,
sh -c 'xsel | xvkbd -file - 2>/dev/null'
works, -xsendevent had to get out to make it working. -
sup about 12 yearsBy the way once more, I had to assign this command to a single key shortcut (luckily I have ThinkVantage button doing nothing on my Thinkpad - ctrl + alt + v behaved strangely and pasted only part of what was in
xsel
). -
Sparhawk over 11 yearsThis works very well except for two problems. (1) I use a drop-down terminal (Guake) that I have set to hide on lose focus. Running this script removes focus temporarily from the terminal, causing it to hide. (2) It doesn't deal well with UTF-8 characters double-byte characters. e.g. if I select "éòü", using the script prints "éòü".
-
Sparhawk over 11 yearsIt looks like the second bug is in
xvkbd
.xsel
works fine with "éòü" in the primary selection, butxvkbd
fails as the script above. (And also fails withxvkbd -text "éòü"
, although here it prints nothing.) -
Peter V. Mørch over 10 yearsHere on Ubuntu 12.04 Precise, I did: Open "Keyboard" from Unity ( == Settings / Keyboard). Shortcuts tab, '+' for Add Shortcut, and assign
sh -c 'xsel | xvkbd -xsendevent -file - 2>/dev/null'
as the command to my chosen key (CTRL-ALT-V
). I also had to remove-xsendevent
. -
ntc2 over 5 yearsThis works, but one less-than-ideal thing about it is that it behaves as if I had literally typed the selection in: there is a delay while the selected text gets entered character by character (vs instantly for a middle click), and undoing the paste also takes many steps.
-
Xen2050 about 5 yearsDoes this save the original clipboard contents & set it again afterwards? Or does it trash the original clipboard contents, thus failing the "to paste without trashing my clipboard contents" part of the question?
-
ntc2 about 5 years@Xen2050 yes, this trashes the CLIPBOARD. I'll add a note to the answer.
-
Pablo Bianchi about 5 yearsOn Ubuntu by default Shift+Insert paste the regular clipboard just like Ctrl+v, not the primary selection. Which distro are you using?
-
MountainX over 4 yearsworks for me in Arch Linux now
-
Florian about 4 yearsI can recommend a variation that also works with international characters:
sh -c 'xsel | iconv -f UTF-8 -t UTF-16 - | xvkbd -xsendevent -utf16 -file - 2>/dev/null'
. This requires theiconv
package to be installed in addition to the other packages from the answer. -
Dragon warrior over 3 yearsI am afraid this is X specific, i.e. does not work on Wayland
-
Dragon warrior over 3 yearsI am afraid this is X specific, i.e. does not work on Wayland
-
Dragon warrior over 3 yearsI am afraid this is X specific, i.e. does not work on Wayland
-
dreua about 2 yearsSolutions for Wayland would be welcome!