Skip to content

Instantly share code, notes, and snippets.

@rehhouari
Forked from varenc/system-wide-clipboard.zsh
Last active March 31, 2024 05:03
Show Gist options
  • Save rehhouari/1890c894cdd1f2dbd33118fb3984fc44 to your computer and use it in GitHub Desktop.
Save rehhouari/1890c894cdd1f2dbd33118fb3984fc44 to your computer and use it in GitHub Desktop.
Zsh copy & paste system wide for Linux (X11& Wayland with customizable paste and copy commands)
# Forked from https://gist.github.com/varenc/e4a22145c484771f254fa20982e2cd7f
## 2024-03-30: Ported it to Linux, added display detection and customizable copy/paste commands
## Using environment variables
## 2020-05-16: Changed `echo` to `printf` so that backslashes are not interpretted by echo
## and so that CUTBUFFER shows up exactly in the pasteboard without modification
# Detect display server
display_server=$(echo $DISPLAY | cut -d: -f1)
# Default commands
: ${ZSHCLIP_COPY_COMMAND:="xclip -sel clip"}
: ${ZSHCLIP_YANK_COMMAND:="xclip -o -sel clip"}
# If running on Wayland, use wl-clipboard
if [ "$display_server" = "wayland" ]; then
ZSHCLIP_COPY_COMMAND="wl-copy"
ZSHCLIP_YANK_COMMAND="wl-paste"
fi
zshclip-copy-to-clipboard () {
printf '%s' $CUTBUFFER | eval $ZSHCLIP_COPY_COMMAND
}
zshclip-yank-from-clipboard () {
CUTBUFFER=$(eval $ZSHCLIP_YANK_COMMAND)
zle yank
}
zshclip-kill-line () {
zle kill-line
zshclip-copy-to-clipboard
}
zshclip-kill-whole-line () {
zle kill-whole-line
zshclip-copy-to-clipboard
}
zshclip-backward-kill-word () {
zle backward-kill-word
zshclip-copy-to-clipboard
}
zshclip-kill-word () {
zle kill-word
zshclip-copy-to-clipboard
}
zshclip-kill-buffer () {
zle kill-buffer
zshclip-copy-to-clipboard
}
zshclip-copy-region-as-kill-deactivate-mark () {
zle copy-region-as-kill
zle set-mark-command -n -1
zshclip-copy-to-clipboard
}
zshclip-yank () {
zshclip-yank-from-clipboard
}
zle -N zshclip-kill-line
zle -N zshclip-kill-whole-line
zle -N zshclip-backward-kill-word
zle -N zshclip-kill-word
zle -N zshclip-kill-buffer
zle -N zshclip-copy-region-as-kill-deactivate-mark
zle -N zshclip-copy-only
zle -N zshclip-yank
bindkey '^K' zshclip-kill-line
## optionally, remove the above and uncomment this only do special pasteboard kill on Ctrl+Alt+k
#bindkey '^[^K' zshclip-kill-line
bindkey '^U' zshclip-kill-whole-line
bindkey '\e^?' zshclip-backward-kill-word
bindkey '\e^H' zshclip-backward-kill-word
bindkey '^W' zshclip-backward-kill-word
bindkey '\ed' zshclip-kill-word
bindkey '\eD' zshclip-kill-word
bindkey '^X^K' zshclip-kill-buffer
bindkey '\ew' zshclip-copy-region-as-kill-deactivate-mark
bindkey '\eW' zshclip-copy-region-as-kill-deactivate-mark
bindkey '^Y' zshclip-yank
# alt-c,v,x shortcuts for copy, paste, cut respectively
bindkey '^[c' zshclip-copy-region-as-kill-deactivate-mark
bindkey '^[v' zshclip-yank
bindkey '^[x' zshclip-kill-line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment