This commit is contained in:
Louis Rubet 2025-12-02 10:06:46 -03:00 committed by GitHub
commit 1b44fc9102
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,17 @@
# zsh-ghostty-copypaste
[ghostty](https://ghostty.org/) users, this `zsh` plugin allows you to copy / paste a text selected with the keyboard with `ctrl+shift+c` / `ctrl+shift+v`.
This is a complement to [zsh-shift-select](https://github.com/jirutka/zsh-shift-select), allowing to copy with `{ctrl+}shift+{left,right,up,down,home,end}`.
To use it, add `ghostty-copypaste` to the plugins array of your `.zshrc` file:
```
plugins=(... ghostty-copypaste)
```
## Principle
- `ghossty` allows to copy / paste a text selected with the mouse, but not with the keyboard, because this is an action devoted to a terminal (`zsh`) and not to a terminal emulator (`ghostty`).
- When no mouse selection is started, `ghostty` passes a Control Sequence Introducer `^[[99;6u` / `^[[118;6u` to `zsh`. The plugin binds these CSI to the correct copy / paste functions for X11 or Wayland.
- Be careful, `ghostty` can lose focus when copying and pasting because of `xclip` or `wl-copy`/`wl-paste` in the plugin. If you use it as a context terminal ("quake"), it is best not to close it when it loses focus.

View file

@ -0,0 +1,42 @@
function zle-copy-to-clipboard {
local selected_text start_pos end_pos
(( $MARK < $CURSOR )) \
&& { start_pos=$MARK; end_pos=$CURSOR; } \
|| { start_pos=$CURSOR; end_pos=$MARK; }
(( start_pos < 0 || end_pos < 0 )) \
&& zle -R && return
selected_text="${BUFFER[$start_pos+1, $end_pos]}"
command -v xclip &> /dev/null \
&& ( echo -n "$selected_text" | xclip -selection clipboard ) \
&& zle -R && return
command -v wl-copy &> /dev/null \
&& ( echo -n "$selected_text" | wl-copy ) && zle -R && return
zle -R
}
function zle-paste-from-clipboard {
local clipboard_content
command -v xclip &> /dev/null \
&& clipboard_content=$(xclip -selection clipboard -o 2>/dev/null)
command -v wl-paste &> /dev/null \
&& clipboard_content=$(wl-paste --no-newline 2>/dev/null)
[[ -z "$clipboard_content" ]] && return
LBUFFER+="$clipboard_content"
zle -R
}
zle -N zle-copy-to-clipboard
zle -N zle-paste-from-clipboard
# these 2 CSI from ghostty are sent for Ctrl+Shift+C and Ctrl+Shift+V
bindkey '^[[99;6u' zle-copy-to-clipboard
bindkey '^[[118;6u' zle-paste-from-clipboard