現在のオペレーティング システムに基づいて Tmux コマンドを実行するにはどうすればよいですか?

現在のオペレーティング システムに基づいて Tmux コマンドを実行するにはどうすればよいですか?

現在のオペレーティング システムに基づいて Tmux コマンドを実行するにはどうすればよいですか?

Linux と macOS の両方で同じ Tmux 構成ファイルを使用したいのですが、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

Tmux で、現在のオペレーティング システム (Linux または macOS) に基づいて条件付きコマンドを実行します。

# 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.

関連情報