Shell

如何根據目前作業系統執行 Tmux 命令?

  • August 10, 2020

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

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

引用自:https://unix.stackexchange.com/questions/603750