Bash's clipboard is internal to bash, bash doesn't connect to the X server.
What you could do is change the meaning of M-w
to copy the selection to the X clipboard¹ in addition to bash's internal clipboard. However bash's integration is pretty loose, and I don't think there's a way to access the region information or the clipboard from bash code. You can make a key binding to copy the whole line to the X clipboard.²
if [[ -n $DISPLAY ]]; then
copy_line_to_x_clipboard () {
printf %s "$READLINE_LINE" | xsel -ib
}
bind -x '"\eW": copy_line_to_x_clipboard'
fi
If you want to do fancy things in the shell, switch to zsh, which (amongst other advantages) has far better integration between the line editor and the scripting language.
if [[ -n $DISPLAY ]]; then
x-copy-region-as-kill () {
zle copy-region-as-kill
print -rn -- "$CUTBUFFER" | xsel -ib
}
x-kill-region () {
zle kill-region
print -rn -- "$CUTBUFFER" | xsel -ib
}
zle -N x-copy-region-as-kill
zle -N x-kill-region
bindkey '\C-w' x-kill-region
bindkey '\ew' x-copy-region-as-kill
fi
¹
Gnome doesn't specifically have a clipboard, this is general to X.
²
As of bash 4.1, there is a bug in the key parsing code: key sequences bound with bind -x
may not be more than two characters long. I think bash 4.2 fixes some cases of longer prefixes but not all of them; I haven't researched the details.
Use the following tmux.conf
with copy-pipe
in the new versions of tmux (1.8+):
set -g mouse on
# To copy:
bind-key -n -t emacs-copy M-w copy-pipe "xclip -i -sel p -f | xclip -i -sel c "
# To paste:
bind-key -n C-y run "xclip -o | tmux load-buffer - ; tmux paste-buffer"
prefix+[
into copy-mode
- select content with mouse(hold)
M-w
to copy that part into system clipboard
C-y
the paste it inside tmux, C-v
to paste it inside other regular application like web browser.
Best Answer
You can perhaps do what you want using bash's readline with
-i
which provides an initial input to-e
edit. For example, usingdate
rather thanls
as it is simpler to see:You now have the string
Mon Jul 25 13:42:47 CEST 2016
as shown with the cursor at the end. You can edit this using the usual cursor keys and so on. For example, you could edit the date25
to20
and then add anecho
to the start, givingWhen you press return, the line read is placed in variable
REPLY
, which you then execute. This is a bit fragile, as the reply is split on spaces and so on. You can add quotes and an eval:Then if you edit the line, changing 25 as before, and insert a command that needs an argument with spaces, eg:
you will get the right answer
1469014967
. As always, beware with eval.