How can I run a Tmux command based on the current operating system?

How can I run a Tmux command based on the current operating system?

How can I run a Tmux command based on the current operating system?

I want to use the same Tmux configuration file on both Linux and macOS, but some configuration like integrating Tmux with the system clipboard is not portable across platforms.

I've read about the if-shell command, but some resources say it's an asynchronous operation (executed in the background), and so the session will likely not be configured properly (why?).

~/.tmux.conf:

# vim copy to system clipboard
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "pbcopy"
#bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"

Respuesta1

Execute a conditional command, based on the current operating system (Linux or macOS), in Tmux:

# vim copy to system clipboard
if-shell '[[ $(uname -s) = Linux ]]' { 
   bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard" 
} { 
   bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "pbcopy" 
}

if-shell supports an option (-b) to run shell-command in the background; by default, it runs immediately:

if-shell [-bF] [-t target-pane] shell-command command [command]
              (alias: if)
        Execute the first command if shell-command returns success or the second command otherwise.  Before being executed, shell-command is expanded using the rules specified in the FORMATS section,
        including those relevant to target-pane.  With -b, shell-command is run in the background.

Also (man tmux):

Commands like if-shell, run-shell and display-panes stop execution of subsequent commands on the queue until something happens - if-shell and run-shell until a shell command finishes and display-panes until a key is pressed.  For example, the following commands:

           new-session; new-window
           if-shell "true" "split-window"
           kill-session

Will execute new-session, new-window, if-shell, the shell command true(1), split-window and kill-session in that order.

información relacionada