
Как запустить команду Tmux на основе текущей операционной системы?
Я хочу использовать один и тот же файл конфигурации Tmux как на Linux, так и на macOS, но некоторые конфигурации, такие как интеграция Tmux с системным буфером обмена, не переносимы на другие платформы.
Я читал об этой if-shell
команде, но некоторые ресурсы говорят, что это асинхронная операция (выполняется в фоновом режиме), и поэтому сеанс, скорее всего, не будет настроен должным образом (почему?).
~/.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"
решение1
Выполнить условную команду в зависимости от текущей операционной системы (Linux или macOS) в 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
поддерживает опцию ( -b
) для работы shell-command
в фоновом режиме; по умолчанию он запускается немедленно:
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.
Также ( 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.