
Wie kann ich einen Tmux-Befehl basierend auf dem aktuellen Betriebssystem ausführen?
Ich möchte dieselbe Tmux-Konfigurationsdatei sowohl unter Linux als auch unter macOS verwenden, aber einige Konfigurationen wie die Integration von Tmux in die Systemzwischenablage sind nicht plattformübergreifend portierbar.
Ich habe über den Befehl gelesen if-shell
, aber einige Ressourcen sagen, dass es sich um eine asynchrone Operation handelt (die im Hintergrund ausgeführt wird), und daher wird die Sitzung wahrscheinlich nicht richtig konfiguriert (Warum?).
~/.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"
Antwort1
Führen Sie in Tmux einen bedingten Befehl aus, basierend auf dem aktuellen Betriebssystem (Linux oder 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
unterstützt eine Option ( -b
) zur Ausführung shell-command
im Hintergrund; standardmäßig wird es sofort ausgeführt:
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.
Auch ( 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.