fish_clipboard_copy: make it work inside SSH/containers via OSC 52

When running inside SSH, Control-X runs a clipboard utility on the remote
system.  For pbcopy (and probably clip.exe too) this means that we write to the
remote system's clipboard. This is usually not what the user wants (although
it is consistent with  fish_clipboard_paste).  When X11 forwarding is used,
xclip/xsel copy to the SSH client's clipboard, which is what most users want.

When we don't have X11 forwarding, we need a different solution. Fortunately,
modern terminal emulators implement the OSC 52 escape sequence for setting
the clipboard of the terminal's system. Use it in fish_clipboard_copy.

Tested in SSH and Docker containers on foot, iTerm2, kitty, tmux and xterm
(this one requires "XTerm.vt100.allowWindowOps: true").

Should also work in GNU screen and Windows Terminal. On terminals that don't
support OSC 52 (like Gnome Terminal or Konsole), it seems to do nothing.

Since there does not seem to be a way to feature-probe OSC 52, let's just
always do both (pbcopy and friends as well as OSC 52).  In future, we should
probably stop calling pbpaste and clip.exe, at least on remote systems.

I think there is also an escape sequence to request pasting the system
clipboard but that's less important and less popular, possibly due to
security concerns.
This commit is contained in:
Johannes Altmanninger 2022-10-09 19:43:55 +02:00
parent db0a297b8a
commit 4de2891507
2 changed files with 12 additions and 0 deletions

View File

@ -83,6 +83,7 @@ Interactive improvements
- Fish now disables the QUIT terminal sequence when it has the terminal. This frees up a key combination, often ctrl-backslash (``\x1c``) (:issue:`9234`).
- Fish's vi mode no longer uses iTerm's proprietary escape sequences to signal cursor change, instead using the normal xterm-style sequences. This allows for a blinking cursor and makes it work in complicated scenarios with nested terminals. (:issue:`3741`, :issue:`9172`)
- Generating descriptions for commands now uses ``manpath`` instead of ``man --path`` on macOS, as that has been removed in macOS Ventura.
- When running fish on a remote system (e.g. inside SSH or a container), :kbd:`Control-X` now copies to the local client system's clipboard if the terminal supports OSC 52.
Fixed Bugs

View File

@ -23,4 +23,15 @@ function fish_clipboard_copy
else if type -q clip.exe
printf '%s' $cmdline | clip.exe
end
# Copy with OSC 52; useful if we are running in an SSH session or in
# a container.
if type -q base64
if not isatty stdout
echo "fish_clipboard_copy: stdout is not a terminal" >&2
return 1
end
set -l encoded (printf %s $cmdline | base64 | string join '')
printf '\e]52;c;%s\a' "$encoded"
end
end