kutsan / zsh-system-clipboard

System clipboard key bindings for Zsh Line Editor with vi mode. It is similar to what `set clipboard=unnamed` does for vim.
GNU General Public License v3.0
149 stars 16 forks source link

Automatically picks "wlc" as the copy handler when the terminal is running under X11 #50

Closed andis-sprinkis closed 8 months ago

andis-sprinkis commented 8 months ago
    linux*|freebsd*)
      if _zsh_system_clipboard_command_exists wl-copy; then
        ZSH_SYSTEM_CLIPBOARD_METHOD="wlc"
      elif _zsh_system_clipboard_command_exists xsel; then
        ZSH_SYSTEM_CLIPBOARD_METHOD="xsc"
      elif _zsh_system_clipboard_command_exists xclip; then
        ZSH_SYSTEM_CLIPBOARD_METHOD="xcc"
....

This extension by default breaks under X11 for those who have installed the i3 WM (X11) with xclip and the Sway WM (Wayland) with wl-clipboard in parallel, as it prioritizes the "wlc".

Here I have terminal running under X11:

1709969149

(I experience this issue with and without tmux.)

I am wondering if there is a robust way to detect the display server in focus and pick the compatible handler, perhaps real-time, without restarting the shell.

andis-sprinkis commented 8 months ago

Not sure how entirely robust it its, but running sway in Linux console tty after login does set $WAYLAND_DISPLAY id variable for each instance, which then can be checked for in the interactive shell init. script (.zshrc).

  # configure zsh-system-clipboard
  [ "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && {
    export ZSH_SYSTEM_CLIPBOARD_METHOD="xcc"
  }

$WAYLAND_DISPLAY doesn't get set or updated in already running tmux instance though, but may be OK if user doesn't expect to run i3 and Sway in parallel.

ErrrorMaxx commented 8 months ago

I use this hook to automatically update environment in tmux:

# Update current tmux shell envirenment (useful for new ssh connections, X-forwarding, etc)
function tmux-update-env-preexec () {
  emulate -L zsh
  if [[ -n "$TMUX" || "$TERM" = tmux* || "$TERM_PROGRAM" = 'tmux' ]]; then
    eval "${(@MF)${(f)$(tmux showenv -s 2> /dev/null)}:#(* |)(DISPLAY|WAYLAND_DISPLAY|SSH_CONNECTION|SSH_AUTH_SOCK)[=;]*}"
  fi
}
add-zsh-hook {,tmux-update-env-}preexec
doronbehar commented 8 months ago

I also try to avoid putting too much logic into the code, see Please adjust ZSH_SYSTEM_CLIPBOARD_METHOD yourway in your personal dotfiles, I'm closing.