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.
stty kill ''
beforebind -x "\C-u"
suggested by @Gilles but the effect is that, onCtrl+U
, I get the^U
itself displayed. No deletion.