2016-05-24 19:17:03 +08:00
|
|
|
function fish_clipboard_copy
|
2022-10-12 02:19:21 +08:00
|
|
|
set -l cmdline
|
|
|
|
if isatty stdin
|
|
|
|
# Copy the current selection, or the entire commandline if that is empty.
|
|
|
|
# Don't use `string collect -N` here - `commandline` adds a newline.
|
|
|
|
set cmdline (commandline --current-selection | string collect)
|
|
|
|
test -n "$cmdline"; or set cmdline (commandline | string collect)
|
|
|
|
else
|
|
|
|
# Read from stdin
|
|
|
|
while read -lz line
|
|
|
|
set -a cmdline $line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
if type -q pbcopy
|
2020-04-21 13:24:20 +08:00
|
|
|
printf '%s' $cmdline | pbcopy
|
2019-10-24 17:41:16 +08:00
|
|
|
else if set -q WAYLAND_DISPLAY; and type -q wl-copy
|
2020-04-21 13:24:20 +08:00
|
|
|
printf '%s' $cmdline | wl-copy
|
2021-07-24 02:50:04 +08:00
|
|
|
else if set -q DISPLAY; and type -q xsel
|
|
|
|
printf '%s' $cmdline | xsel --clipboard
|
|
|
|
else if set -q DISPLAY; and type -q xclip
|
|
|
|
printf '%s' $cmdline | xclip -selection clipboard
|
2020-11-06 20:11:56 +08:00
|
|
|
else if type -q clip.exe
|
|
|
|
printf '%s' $cmdline | clip.exe
|
2016-05-24 19:17:03 +08:00
|
|
|
end
|
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.
2022-10-10 01:43:55 +08:00
|
|
|
|
|
|
|
# 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"
|
2022-10-23 22:47:39 +08:00
|
|
|
# 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
|
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.
2022-10-10 01:43:55 +08:00
|
|
|
end
|
2016-05-24 19:17:03 +08:00
|
|
|
end
|