以操作系统为条件的 tmux 配置

muc*_*out 49 tmux

tmux.conf只有当我的操作系统是 Mac 时,我才会执行一些我的代码。但是,我想tmux.conf在多个不同的操作系统上使用我的。如何使命令以 tmux 当前运行的操作系统为条件?

che*_*ner 57

使用if-shell命令:

if-shell "uname | grep -q Darwin" "tmux-cmd1; tmux-cmd2;" "tmux-cmd3; tmux-cmd4"
Run Code Online (Sandbox Code Playgroud)

您可能希望将特定于操作系统的命令放在单独的文件中,并通过“源文件”命令执行它们。

if-shell "uname | grep -q Darwin" "source-file .tmux-macosx" "source-file .tmux-linux"
Run Code Online (Sandbox Code Playgroud)

  • `if-shell` 和 `run-shell` *tmux* 命令目前是异步的(从 *tmux* 1.7 开始);他们在后台有效地运行他们的 shell 命令,并且他们运行的任何 *tmux* 命令只会在**之后**任何在 `if-shell` 或 `run-shell` 命令本身之后的命令(*tmux * 是单线程的)。实际上,如果你在 `~/.tmux.conf` 中使用 `if-shell` 或 `run-shell`,初始会话(以及通过 `~/tmux.conf` 显式创建的任何会话、窗口或窗格)将缺少通过 `if-shell` 或 `run-shell` 命令安排的任何 *tmux* 配置。 (8认同)
  • 这应该被接受;这是正确的方法。 (2认同)

Iva*_*van 13

Jimeh https://github.com/jimeh/dotfiles/commit/3838db8有答案。此外,Chris Johnsen 因在 GitHub 问题上帮助人们而值得称赞:https : //Github.com/ChrisJohnsen/tmux-MacOSX-pasteboard/issues/8#issuecomment-4134987

基本上,您设置了一个名为的 shell 脚本safe-reattach-to-user-namespace,用于检查是否存在真正的 reattach... 命令。

#! /usr/bin/env bash

# If reattach-to-user-namespace is not available, just run the command.
if [ -n "$(command -v reattach-to-user-namespace)" ]; then
  reattach-to-user-namespace $@
else
  exec "$@"
fi
Run Code Online (Sandbox Code Playgroud)