Linux – True Copy/Paste shortcut remapping in X11

copy/pastekeyboard shortcutslinuxvimxorg

Can I use CMD-C and CMD-V to trigger the X11 clipboard functionality?

I do not want to remap CMD to send CTRL. This is not just about ergonomics and muscle memory. Existing answers seem to ignore the fact that ^C is a valid, useful character with its own meaning to lots of programs. I want to bind window manager commands (like copy/paste) to key combinations that don't already have decades-old conventions associated with them in popular programs, like Vim and Emacs. Something like CMD-C.

I want my CTRL key to keep doing what it was designed to do.

  • CTRL-C should continue to send an ^c character to my terminal to kill processes
  • CTRL-V should continue to send a ^v character (the escape sequence in Vim)
  • CTRL-X should continue to send a ^x character (vim's key for "subtract count from number under cursor)

I want to use the CMD key to send commands to my window manager:

  • CMD-C should set the highlighted text/object to the X11 CLIPBOARD selection.
  • CMD-V should paste from the X11 CLIPBOARD buffer -no matter what is in there– into the active target, like CTL-V or Edit->Paste does by default in almost every X application.

MacOS does system-wide shortcuts really well. It took some muscle memory adjustment, but now that I've realized that I can send actual control characters to my applications, I can't ever go back.

Can I configure my linux box to do the same thing, at least for copy & paste? The biggest sticking point is that CTRL-C/CTRL-V for copy/paste conflicts with vim keybindings, but generally I'd like to separate terminal control characters from window-manager command sequences.

There's a bit of useful documentation on Jamie Zawinski's site on the underlying mechanisms for how X11 handles copy/paste buffers. This is exactly the functionality I want to directly control with my keyboard. How do I configure it?

Best Answer

This is a hard problem because a lot of apps on Linux try to be smart about it and re-implement the logic on their side. Also because the concept of clipboard in X11 is broader and has several ones with different behaviors.

My setup consists of using a keybinding daemon and execute commands whenever CMD-C and CMD-V are used. The only drawback so far is CMD-V requires the user to release V first to get the paste applied. So you cannot "hold repeat" it, neither can you release CMD first (might trick your muscle memory at first).

First, create a script like this one:

#!/bin/bash
active_window_id=$(xdotool getactivewindow)
active_window_pid=$(xdotool getwindowpid "$active_window_id")
process=$(cat /proc/$active_window_pid/comm)
if [[ "$process" == "xterm" ]]; then
  xdotool key Control_L+Shift_L+v
else
  xdotool key --clearmodifiers Control_L+v
fi

Then, an example of valid config file for xbindkeys:

"xsel -po|xsel -bi"
  Mod4+c

"/path/to/your/script.sh"
  Release+Mod4+v

Example of valid config file for sxhkd:

super + c
  xsel -po|xsel -bi

@super + v
  /path/to/your/script.sh

You can use your WM config to map the commands too, and map additional ones, like CMD+A for "select all", CMD+X for cut, and so on.