40

How do I paste from the PRIMARY selection (eg. mouse-selected text) with a keyboard shortcut? Shift+Insert inconsistently pastes from PRIMARY or CLIPBOARD, depending on the application.

Background:

Ctrl+C copies selected text to CLIPBOARD while mouse-selection copies to PRIMARY. Paste from CLIPBOARD with Ctrl+V and paste from PRIMARY with mouse-middle-click.

In a terminal emulator (gnome-terminal), paste from CLIPBOARD with Ctrl+Shift+V. (Paste from PRIMARY with mouse-middle-click still.)

I want to paste from PRIMARY with a keyboard shortcut. In gnome-terminal, this is Shift+Insert, but in gedit and Firefox, Shift+Insert pastes from CLIPBOARD. I want a shortcut that consistently pastes from CLIPBOARD and a different short cut that consistently pastes from PRIMARY.

I'm running Ubuntu 14.04 with xmonad and Firefox 34.0

reasgt
  • 793

2 Answers2

38

All the apps you've mentioned are gtk+ apps so it's quite easy to answer Why... Because in all gtk+ apps (except one), Shift+Insert pastes from CLIPBOARD - i.e. it's equivalent to Ctrl+V. The shortcut is hardcoded in gtkentry.c (line 2022) and gtktextview.c (line 1819):

gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, GDK_SHIFT_MASK,
                "paste-clipboard", 0);

It is also documented in the GTK+ 3 Reference Manual under GtkEntry:

The “paste-clipboard” signal
void
user_function (GtkEntry *entry,
               gpointer  user_data)
The ::paste-clipboard signal is a keybinding signal which gets emitted
to paste the contents of the clipboard into the text view.
The default bindings for this signal are Ctrl-v and Shift-Insert.

As far as I know this was done for consistency with other DE's (see KDE's Qt key bindings in QTextEdit Class) and Windows OS1.
The only exception is gnome-terminal. After long debates, the devs have decided (for consistency with other terminals) that, in gnome-terminal, Shift+Insert should paste from PRIMARY and Ctrl+Shift+V should paste from CLIPBOARD (although you have the options to customize some shortcuts).


As to How do you paste selection with a keyboard shortcut... there's no straightforward way.

The easiest way is to assign a shortcut to a script that runs xdotool click 2 (simulates clicking the middle-mouse button). While this works (and it should work with all or most DE's and toolkits), it only works if the mouse cursor is actually over the text entry box, otherwise it fails.

Another relatively easy way is via Gnome Accessibility, if it's available on your system. It also requires the presence of a numpad. Go to Universal Access >> Pointing & Clicking and enable Mouse Keys. Make sure NumLock is off. You can then use the numpad keys to move the cursor and click. To simulate a middle-mouse button click, press (and release) * (asterisk) then press 5 (here's a short guide). This solution seems to always work in a gtk+ environment. The downside is that it requires Gnome Accessibility and a numpad. Also, you cannot customize the shortcut.

An interesting solution was proposed on gnome-bugzilla (bug 643391). (Update 2018: issue has now been moved here.) It requires patching some source files and setting configuration options in ~/.config/gtk-3.0/gtk.css (or ~/.gtkrc-2.0 for gtk+ 2 apps). I haven't tried it personally but the feedback is positive.

Ideally, you would patch the source files and define a "paste-selection" signal then bind Shift+Insert to "paste-selection" instead of "paste-clipboard". Andy's code (attached in the bug report linked above) could serve as a guide on how to do that. Even then, it would only affect gtk+ apps (I'm not a KDE/Qt guy so I have no idea how to alter Qt apps behavior).


1: (not to mention IBM's CUA)

dctucker
  • 103
don_crissti
  • 82,805
  • 3
    Thanks for that explanation. It's a pity there exists no better solution, I miss a consistent keyboard shortcut for PRIMARY a lot. The mouse emulation workaround doesn't help much as the point of the shortcut is to avoid having to deal with the mouse. – didi_X8 May 14 '18 at 08:37
2

I am sorry: I can not write comment because of reputation, so I write as answer. It is not direct solution, but it helps me...

You can use combination of information:

  1. Edit gtk theme and allow key binding as you wish.

  2. Example of creating gtk 3.0 keybinding in css of theme.

  3. Example of overriding gtk keybindings and unbind.

  4. Example of modify keybinding for gnome-terminal

  5. Auto start of autocutsel with default options for sync CLIPBOARD to CUTBUFFER (this is optional). This link is only for information about problem.

  6. Use clipman to gui access multiple buffers (PRIMARY and CLIPBOARD), without auto sync selections (bi-directional auto sync makes wrong).

So I just select gtk theme, and modify theme CSS to needed keybindings for copy-clipboard (Ctrl+Insert) and paste-clipboard (Shift+Insert) signals.

Toby Speight
  • 8,678
  • 3
    This could be the start of a good answer, if you take the contents of those links and combine the information. As it stands, it's just a collection of links, and that puts it at risk of being deleted. Please fill in the information so it's directly in the answer, and I'm sure you'll get upvotes (thereby giving you enough reputation to comment). Thanks! – Toby Speight Sep 17 '19 at 10:42