如何根據目前作業系統運行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

根據目前作業系統(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.

相關內容