Skip to content

Instantly share code, notes, and snippets.

@tavisrudd
Created August 24, 2011 20:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tavisrudd/1169093 to your computer and use it in GitHub Desktop.
Save tavisrudd/1169093 to your computer and use it in GitHub Desktop.
bash readline integration with clipboards: M-k = kill, M-y = yank, M-u = backwards kill
## derived from http://stackoverflow.com/questions/994563/integrate-readlines-kill-ring-and-the-x11-clipboard
## You need to choose which clipboard to integrate with, OS X or X11:
shell_copy() {
pbcopy # OS X
# or ssh user@remote_mac pbcopy
# or xclip
}
shell_yank() {
pbpaste
# or ssh user@remote_mac pbpaste
# or xclip -o
}
# in the above you could also copy and yank to gnu screen's paste buffer or even remote shared clipboards.
# Just pipe to wherever you want!
## then wire up readline
_xdiscard() {
echo -n "${READLINE_LINE:0:$READLINE_POINT}" | shell_copy
READLINE_LINE="${READLINE_LINE:$READLINE_POINT}"
READLINE_POINT=0
}
_xkill() {
echo -n "${READLINE_LINE:$READLINE_POINT}" | shell_copy
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}"
}
_xyank() {
CLIP=$(shell_yank)
COUNT=$(echo -n "$CLIP" | wc -c)
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}${CLIP}${READLINE_LINE:$READLINE_POINT}"
READLINE_POINT=$(($READLINE_POINT + $COUNT))
}
bind -m emacs -x '"\eu": _xdiscard' # backwards kill from point
bind -m emacs -x '"\ek": _xkill'
bind -m emacs -x '"\ey": _xyank'
@jsomedon
Copy link

Hi Tavis,

I'm regularly ssh-ing between mac and linux machines, and I want to use these commands over ssh. How shell_copy and shell_yank would look like? Does the implementation depend on OS of the machine that I am: ssh-ing to or ssh-ing from? Or both?

@Boruch-Baum
Copy link

Credit would seem to be due to ephemient and Wei Hu, no?

@jasonm23
Copy link

@Boruch-Baum - the Stack overflow Q&A was always linked in the top of the Gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment