47

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

3
  • 2
    Possible same any distro: unix.stackexchange.com/questions/11889/… Apr 9, 2015 at 10:13
  • FWIW, 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 Aug 4, 2017 at 16:16
  • 2
    @JonathanHartley for me shift-insert pastes from the clipboard not primary selection
    – jberryman
    Jun 25, 2019 at 20:25

9 Answers 9

34

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
10
  • 3
    Holy bananas! It works! Thats awesome. Ubuntu should set this up by default. Jan 13, 2011 at 1:01
  • 10
    For me, sh -c 'xsel | xvkbd -file - 2>/dev/null' works, -xsendevent had to get out to make it working.
    – sup
    Mar 11, 2012 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, 2012 at 10:50
  • 2
    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, 2013 at 23:18
  • 2
    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, 2013 at 23:38
10

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.

1
8

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.

9
  • 1
    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, 2010 at 19:11
  • 1
    @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... Oct 16, 2010 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, 2010 at 19:29
  • @Roger: Got it correct second time round: xdotool+xsel does it. (Well, at least works for me.) Oct 16, 2010 at 19:36
  • 2
    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, 2010 at 20:52
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...

3

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 used xdotool 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)

2
  • works for me in Arch Linux now
    – MountainX
    Nov 4, 2019 at 4:38
  • I am afraid this is X specific, i.e. does not work on Wayland Nov 19, 2020 at 17:54
2

I had problems with the solutions that simulate pasting the PRIMARY selection, so instead I added a shortcut that copies the PRIMARY selection to the CLIPBOARD. After using my shortcut I paste the CLIPBOARD in the usual way, with Ctrl+V, Ctrl+Shift+V, or Shift+Insert, depending on the application.

The command to copy the PRIMARY selection to the CLIPBOARD is

sh -c 'xsel --output --primary | xsel --input --cliboard'

which I bind to Ctrl+Insert (arbitrary choice) in the Gnome shortcut settings under System Settings -> Devices -> Keyboard

Summary of problems with other solutions: I tried the xsel | xvkbd ... and xsel | xdotool ... solutions, but found they didn't work very well because they simulate typing the PRIMARY selection character by character, which is not the same as pasting it with middle click. Problems include: there's a long delay while a large selection gets inserted one character at a time; if you want to undo the "paste", you have to undo it character by character, which is slow; if you're using "dead keys" then everything gets messed up, e.g. "pasting" "e produces ë; the xvkdb doesn't handle unicode characters correctly.

Warning: This solution overwrites the existing CLIPBOARD contents, but the OP asked for solutions that preserve the existing CLIPBOARD contents.

3
  • 1
    Does 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?
    – Xen2050
    Mar 7, 2019 at 15:41
  • 1
    @Xen2050 yes, this trashes the CLIPBOARD. I'll add a note to the answer.
    – ntc2
    Mar 7, 2019 at 19:52
  • It's not that much harder to write a little shell script or one-liner that switches the contents of the X selection with the clipboard, using xsel (or xclip). If you bind this to a key command, then by combining it with Ctrl+v you can effectively paste the X selection while preserving the existing clipboard contents. Mar 21 at 20:21
0

I had the same issue and internet search didn't help me much. The problem simulating click 2 is annoying as OP mentioned.

The problem with the above proposed xdotool and xsel is when xdotool starts "typing" you are still pressing another key. That does not always result with any output. For example if you bind it to "insert" key then xdotool is sending keys while your finger is pressing "insert" key which causes nothing.

Below is a workaround, to bind it to a key press :

sh -c 'sleep 0.3 && xdotool type --clearmodifiers --delay 0  "`xsel`"'

it is not perfect but working. now you have 0.3 seconds to finish your selection of key press (and key up).

0

You can also use xclip

Copy the content of primary to clipboard using the following command:

sh -c 'xclip -o -selection primary | xclip -selection clipboard'

You can bind this command to any keyboard shortcut of your preference. If you are using Gnome desktop environment you can do it by going to Settings > Keyboard Shortcuts or search for keyboard shortcuts in case of other DE.

Now, you can paste the content in the usual way by Ctrl-v or Ctrl-Shift-v.

HOWEVER This will overwrite the previous content of your clipboard!

0

In KDE on Debian Bullseye the default key combo seems to be Ctrl+Shift+Insert.

You must log in to answer this question.