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
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
^?, but more generally it means that bash's keybindings for all the characters listed by
stty -a (i.e.
^?) 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 ''
printf %s "$READLINE_LINE" | xsel -i;
bind -x '"\C-u": copy_line_to_x_clipboard';
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.