
Como posso executar um comando Tmux baseado no sistema operacional atual?
Quero usar o mesmo arquivo de configuração do Tmux no Linux e no macOS, mas algumas configurações, como a integração do Tmux com a área de transferência do sistema, não são portáteis entre plataformas.
Eu li sobre o if-shell
comando, mas alguns recursos dizem que é uma operação assíncrona (executada em segundo plano) e, portanto, a sessão provavelmente não será configurada corretamente (por que?).
~/.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"
Responder1
Execute um comando condicional, baseado no sistema operacional atual (Linux ou macOS), no 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
suporta uma opção ( -b
) para execução shell-command
em segundo plano; por padrão, ele é executado imediatamente:
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.
Também ( 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.