Skip to content

Instantly share code, notes, and snippets.

@jclosure
Created February 16, 2022 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jclosure/9ce8213d055741596d8a42360bd5596f to your computer and use it in GitHub Desktop.
Save jclosure/9ce8213d055741596d8a42360bd5596f to your computer and use it in GitHub Desktop.
Perfect blend of system clipboard with zsh killring
cutbuffer () {
emulate -L zsh
zle kill-region
zle set-mark-command -n -1
killring=("$CUTBUFFER" "${(@)killring[1,-2]}")
if which clipcopy &>/dev/null; then
printf "%s" "$CUTBUFFER" | clipcopy
else
echo "clipcopy function not found. Please make sure you have Oh My Zsh installed correctly."
fi
}
copybuffer () {
emulate -L zsh
zle copy-region-as-kill
zle set-mark-command -n -1
killring=("$CUTBUFFER" "${(@)killring[1,-2]}")
if which clipcopy &>/dev/null; then
printf "%s" "$CUTBUFFER" | clipcopy
else
echo "clipcopy function not found. Please make sure you have Oh My Zsh installed correctly."
fi
}
pastebuffer () {
if which clippaste &>/dev/null; then
local pasted=$(clippaste)
if [[ $pasted != $CUTBUFFER ]]; then
CUTBUFFER=${pasted}
killring=("$CUTBUFFER" "${(@)killring[1,-2]}")
fi
else
echo "clippaste function not found. Please make sure you have Oh My Zsh installed correctly."
fi
zle yank
}
zle -N copybuffer
zle -N pastebuffer
zle -N cutbuffer
bindkey '\ew' copybuffer
bindkey '\eW' copybuffer
bindkey '^Y' pastebuffer
bindkey '^w' cutbuffer
## for debugging
# `ESC o` to show these buffers any time during typing
function _showbuffers()
{
local nl=$'\n' kr
typeset -T kr KR $'\n'
KR=($killring)
typeset +g -a buffers
buffers+=" Pre: ${PREBUFFER:-$nl}"
buffers+=" Buffer: $BUFFER$nl"
buffers+=" Cut: $CUTBUFFER$nl"
buffers+=" L: $LBUFFER$nl"
buffers+=" R: $RBUFFER$nl"
buffers+="Killring:$nl$nl$kr"
zle -M "$buffers"
}
zle -N showbuffers _showbuffers
bindkey "^[o" showbuffers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment