Skip to content

Instantly share code, notes, and snippets.

@rehhouari
Forked from varenc/system-wide-clipboard.zsh
Last active December 21, 2024 17:12
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-08-26: Update wayland detection method to work in Zellij and over SSH.
## 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
## If running on Wayland, use wl-clipboard
if [[ -z $(loginctl show-session $(loginctl | grep $(whoami) | awk '{print $1}') -p Type | grep wayland) ]]; then
export ZSHCLIP_COPY_COMMAND="xclip -sel clip"
export ZSHCLIP_YANK_COMMAND="xclip -o -sel clip"
else
export ZSHCLIP_COPY_COMMAND="wl-copy"
export 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
bindkey '^H' backward-delete-word
#!/usr/bin/zsh
## universal copy script
$ZSHCLIP_COPY_COMMAND $1
#!/usr/bin/zsh
## universal paste script
$ZSHCLIP_YANK_COMMAND
@xuhdev
Copy link

xuhdev commented Dec 21, 2024

Is zshclip-copy-only undefined?

@rehhouari
Copy link
Author

Is zshclip-copy-only undefined?

yes it is thanks for pointing it out I missed it somehow, but althought I just tried to create it without the "zle set-mark-command -n -1" (deactivate mark) portion, the result would be the same

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