From 90b2c95bbcdb2976deb57d904257d2896dfa9d60 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 23 Oct 2022 16:47:39 +0200 Subject: [PATCH] fish_clipboard_copy: bypass tmux, write OSC 52 to the underlying terminal For security reasons, some terminals require explicit permission from the user to interpret OSC 52. One of them is [tmux] but that one usually runs inside another terminal. This means we can usually write directly to the underlying terminal, bypassing tmux and the need for user configuration. This only works if the underlying terminal is writable to the fish user, which may not be the case if we switched user. For this reason, keep writing to stdout as well, which should work fine if tmux is configured correctly. [tmux]: https://github.com/tmux/tmux/wiki/Clipboard --- share/functions/fish_clipboard_copy.fish | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/share/functions/fish_clipboard_copy.fish b/share/functions/fish_clipboard_copy.fish index 17cb74984..0cf5f15a0 100644 --- a/share/functions/fish_clipboard_copy.fish +++ b/share/functions/fish_clipboard_copy.fish @@ -33,5 +33,16 @@ function fish_clipboard_copy end set -l encoded (printf %s $cmdline | base64 | string join '') printf '\e]52;c;%s\a' "$encoded" + # tmux requires user configuration to interpret OSC 52 on stdout. + # Luckily we can still make this work for the common case by bypassing + # tmux and writing to its underlying terminal. + if set -q TMUX + set -l tmux_tty (tmux display-message -p '#{client_tty}') + or return 1 + # The terminal might not be writable if we switched user. + if test -w $tmux_tty + printf '\e]52;c;%s\a' "$encoded" >$tmux_tty + end + end end end