Skip to content

Instantly share code, notes, and snippets.

@welldan97
Created March 10, 2013 09:51
Show Gist options
  • Save welldan97/5127861 to your computer and use it in GitHub Desktop.
Save welldan97/5127861 to your computer and use it in GitHub Desktop.
Zsh copy & paste system wide for OS X, like in emacs
pb-kill-line () {
zle kill-line
echo -n $CUTBUFFER | pbcopy
}
pb-kill-whole-line () {
zle kill-whole-line
echo -n $CUTBUFFER | pbcopy
}
pb-backward-kill-word () {
zle backward-kill-word
echo -n $CUTBUFFER | pbcopy
}
pb-kill-word () {
zle kill-word
echo -n $CUTBUFFER | pbcopy
}
pb-kill-buffer () {
zle kill-buffer
echo -n $CUTBUFFER | pbcopy
}
pb-copy-region-as-kill-deactivate-mark () {
zle copy-region-as-kill
zle set-mark-command -n -1
echo -n $CUTBUFFER | pbcopy
}
pb-yank () {
CUTBUFFER=$(pbpaste)
zle yank
}
zle -N pb-kill-line
zle -N pb-kill-whole-line
zle -N pb-backward-kill-word
zle -N pb-kill-word
zle -N pb-kill-buffer
zle -N pb-copy-region-as-kill-deactivate-mark
zle -N pb-yank
bindkey '^K' pb-kill-line
bindkey '^U' pb-kill-whole-line
bindkey '\e^?' pb-backward-kill-word
bindkey '\e^H' pb-backward-kill-word
bindkey '^W' pb-backward-kill-word
bindkey '\ed' pb-kill-word
bindkey '\eD' pb-kill-word
bindkey '^X^K' pb-kill-buffer
bindkey '\ew' pb-copy-region-as-kill-deactivate-mark
bindkey '\eW' pb-copy-region-as-kill-deactivate-mark
bindkey '^Y' pb-yank
@avatar-lavventura
Copy link

Thank you! This is pretty helpful

@varenc
Copy link

varenc commented May 16, 2021

I've been using a modified version of this for years and I love! But I now have one minor improvement:

The trouble is that echo interprets backslashes. So if you kill the line \\n\\nhello it'll show up in your pasteboard as \n\nhello.
The simple fix for this is just to use printf '%s' $CUTBUFFER | pbcopy instead of echo.

Made this tiny change in my fork here: https://gist.github.com/varenc/e4a22145c484771f254fa20982e2cd7f

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