Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.)

share|improve this question

3 Answers 3

up vote 14 down vote accepted

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. 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
share|improve this answer
2  
Holy bananas! It works! Thats awesome. Ubuntu should set this up by default. –  Eric Johnson Jan 13 '11 at 1:01
4  
For me, sh -c 'xsel | xvkbd -file - 2>/dev/null' works, -xsendevent had to get out to make it working. –  sup Mar 11 '12 at 11:46
    
By 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). –  sup Mar 13 '12 at 10:50
    
This 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 Jan 30 '13 at 23:18
    
It looks like the second bug is in xvkbd. xsel works fine with "éòü" in the primary selection, but xvkbd fails as the script above. (And also fails withxvkbd -text "éòü", although here it prints nothing.) –  Sparhawk Jan 30 '13 at 23:38

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.

share|improve this answer
    
That 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. –  Roger Pate Oct 16 '10 at 19:11
    
@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... –  Riccardo Murri Oct 16 '10 at 19:24
    
Well, 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. :) –  Roger Pate Oct 16 '10 at 19:29
    
@Roger: Got it correct second time round: xdotool+xsel does it. (Well, at least works for me.) –  Riccardo Murri Oct 16 '10 at 19:36
1  
Needs --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). –  Roger Pate Oct 16 '10 at 20:52

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...

share|improve this answer

Your Answer

 
discard

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