2

There is a related answer (link below), but that requires set-mark. Ctrl+U works differently. \Cu or \C-u can't just be substituted for the \eW (code below).

Here's what Share the clipboard between bash and X11 suggests.

if [[ -n $DISPLAY ]]; then
  copy_line_to_x_clipboard () {
    printf %s "$READLINE_LINE" | xsel -ib
  }
  bind -x '"\eW": copy_line_to_x_clipboard'
fi

So the question is: how do I make Ctrl+U in bash add text that it cuts to Xorg's mouse (middle-click) paste buffer?

1
  • I tried the stty kill '' before bind -x "\C-u" suggested by @Gilles but the effect is that, on Ctrl+U, I get the ^U itself displayed. No deletion.
    – argle
    Feb 11, 2018 at 21:48

1 Answer 1

1

Your function does copy the line to the clipboard.

To use the mouse paste buffer rather than the Ctrl+C/Ctrl+V clipboard, run xsel without the -b option.

To cut rather than copy, delete the text afterwards: set READLINE_LINE to an empty string.

Bash gives terminal settings set by stty precedence over its own key bindings. I guess the intent is primarily to obey the terminal's settings for whether BackSpace sends ^H or ^?, but more generally it means that bash's keybindings for all the characters listed by stty -a (i.e. ^C, ^D, ^H, ^Q, ^S, ^W, ^Z, ^\ and ^?) don't get used by default. You need to unset the stty setting for the control character you want to rebind.

This works for me with the following code in ~/.bashrc with bash 4.3 on Ubuntu 16.04.

if [[ -n $DISPLAY ]]; then
  stty kill ''
  copy_line_to_x_clipboard() {
    printf %s "$READLINE_LINE" | xsel -i;
    READLINE_LINE= READLINE_POINT=0
  };
  bind -x '"\C-u": copy_line_to_x_clipboard';
fi

It doesn't work if I just paste this at the command line: I get the effect you mention in a comment, namely, pressing ^U inserts a literal ^U. It works if I run stty kill '', then bind -x … in a subsequent command.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .