如何根据当前操作系统运行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.