新功能hivemux swarm:Claude Code 的构建和测试在你的其他 Mac 上运行 →

如何在 Ghostty 中配合 tmux 使用 Claude Code

简短回答

把 Ghostty 的 command 设为一个连接 tmux 的脚本,这样每个窗口都会在 tmux 中打开,关闭 Ghostty 也绝不会停止 Claude Code。再加上 macos-option-as-alt = true,让 Option 快捷键能传到 tmux 和 Claude。hivemux setup 会替你写好这些配置行,仓库中的 examples/ghostty.conf 也列出了它们。

作者 · · 7 分钟阅读

Ghostty 是一款适用于 macOS 和 Linux 的快速原生终端,使用纯文本配置文件。单独使用时,它和其他终端一样:关闭窗口,其中的程序就会停止。与 tmux 搭配后,它就成了长期运行会话的外框,这正是需要运行一段时间或等待你批准的 Claude Code 任务所需要的。本指南介绍重要的 Ghostty 设置,以及 hivemux 如何使用它们。

为什么选 Ghostty 搭配 Claude Code 和 tmux

只要你自己启动 tmux,hivemux 在任何终端中都能工作,但它推荐 Ghostty 是有具体原因的:

  • 启动命令。 Ghostty 的 command 设置会在每个新窗口和标签页中运行一个程序。把它指向一个连接 tmux 的脚本,你就永远不会意外落到一个裸 shell 中。
  • Option 作为 Alt。 macos-option-as-alt 让 Option 键充当 Alt/Meta,于是 Option+1 … 9 等 Option 快捷键会传到 tmux 和 Claude Code,而不是输入特殊字符。
  • 来自终端的桌面通知。 hivemux 以 OSC 777 终端转义序列发送通知。Ghostty 会把它们转成 macOS 通知;不支持 OSC 777 的终端不会显示。
  • Claude Code 直接支持。 Anthropic 的终端文档说明 Shift+Enter 在 Ghostty 中无需配置即可使用,Claude Code 的默认通知渠道也能在 Ghostty 中显示桌面通知(preferredNotifChannel 设置还有一个专门的 "ghostty" 值)。通过 SSH 时,Ghostty 也会把这些通知转发到你本地的通知中心。
  • 可脚本化的窗口。 hivemux connect 会在一个新的 Ghostty 窗口中打开远程 Mac 的会话。

第 1 步:安装 Ghostty 和 tmux

Terminal window
brew install --cask ghostty
brew install tmux

也可以跳过 brew install --cask ghostty:缺少 Ghostty 时,hivemux setup 会询问是否用 Homebrew 安装(使用 --yes 时绝不会自动安装)。

Ghostty 的配置文件是 ~/.config/ghostty/config。如果不存在,就创建它。

第 2 步:让每个窗口都连接到 tmux

核心思路只有一行:运行一个 attach 脚本,而不是登录 shell。最简单的脚本可以是 tmux new-session -A -s main,它会连接到名为 main 的会话,没有就创建。hivemux 自带一个更聪明的脚本 hivemux-attach:

  • 没有 tmux 服务器在运行时(重启之后),它会启动一个,恢复最近一次 tmux-resurrect 快照并让每个 Claude 继续之前的对话,然后连接到你上次查看的会话。
  • 已有服务器在运行时,它会连接到没有被其他窗口显示的最近使用的会话;如果所有会话都已在屏幕上,就新建一个带编号的会话。

所以在 Ghostty 中按 Cmd+T 或 Cmd+N,得到的是 tmux 的另一个视图,而不是一个隔离的新环境。

第 3 步:添加 hivemux 的 Ghostty 设置

hivemux setup 会询问 “Make every Ghostty window open inside tmux (hivemux-attach)?”,回答 yes 后,会在 # >>> hivemux >>> 标记之间加入下面这段配置。仓库中的 examples/ghostty.conf 也有同样的内容:

Terminal window
# 每个 Ghostty 窗口/标签页都连接到 tmux,关闭 Ghostty 绝不会中断工作
command = /path/to/hivemux/bin/hivemux-attach
# Option+1..9、Option+0、Option+O 以及 Claude Code 的 Option 快捷键
macos-option-as-alt = true
window-save-state = always
confirm-close-surface = false

每一行的作用:

设置 效果
command 每个窗口和标签页都运行 hivemux-attach,而不是普通 shell
macos-option-as-alt = true Option 充当 Alt,Option 快捷键能传到 tmux 和 Claude Code
window-save-state = always Ghostty 再次启动时重新打开之前的窗口
confirm-close-surface = false 关闭时不再询问“确定吗”;反正 tmux 会保住工作

安装 hivemux 并运行 setup:

Terminal window
curl -fsSL https://hivemux.dev/install.sh | bash

或使用 Homebrew:

Terminal window
brew install hivemux/tap/hivemux
hivemux setup

要跳过 Ghostty 部分,使用 hivemux setup --no-ghostty。hivemux uninstall 只会删除带标记的配置块。

第 4 步:完全重启 Ghostty

已经打开的 Ghostty 窗口会保留旧的 Option 行为。用 Cmd+Q 退出 Ghostty 再重新打开。你应该会在顶部看到一条状态栏,左侧是 hivemux 侧边栏:这说明你已经在 tmux 中了。

运行 tmux 的 Ghostty 窗口,hivemux 侧边栏按项目列出 Claude Code 会话

首次安装后不需要这样做:hivemux setup 完成后会打开 Ghostty,第一个窗口会先打开一次“新建项目”的文件夹选择器。选一个文件夹,按 Enter,Claude 就会在里面启动。

之后的项目,输入 hm 加上文件夹即可(或在侧边栏上右键 →“新建项目”):

Terminal window
hm ~/code/api

Option+1 跳到侧边栏中的第一个 Claude,Option+0 跳到等待最久的那个。如果 Option 快捷键仍然没有反应,先按 Ctrl+A 再按 Space,效果与 Option+0 相同。

第 5 步:允许通知

当某个 Claude 需要输入或完成任务时,hivemux 会通知你,你正在查看的那个除外。在 macOS 中打开“系统设置”→“通知”,允许 Ghostty。在侧边栏中,右键 →“设置”→“通知”必须是打开的。完整说明见 如何在 Claude Code 需要输入或完成时收到通知。

可选:字体与中日韩文字

字体在 Ghostty 中设置,作用于整个终端,包括侧边栏。先查到已安装字体的名称,再在 hivemux 配置块之外添加配置行:

Terminal window
/Applications/Ghostty.app/Contents/MacOS/ghostty +list-fonts | grep -v "^ " | sort -u
Terminal window
font-family = "JetBrains Mono"
font-size = 15
# 第一个字体缺少的字符会使用第二个 font-family
# font-family = "D2Coding"

按 Cmd+Shift+, 重新加载。字体越大,列数越少,所以你可能需要把侧边栏调窄一些(右键 →“设置”→“侧边栏宽度”)。

使用其他终端

iTerm2、WezTerm、kitty、Alacritty 和 Terminal.app 也可以实现同样的自动连接:

Terminal window
hivemux terminal iterm # 或 wezterm、kitty、alacritty、terminal
hivemux terminal iterm --off # 停用

它会在该终端的配置中写入一个带标记的配置块(iTerm2 会新建一个专用的描述文件并设为默认;Terminal.app 则是在 ~/.zshrc 中写入一个配置块,并需要你自己打开 Use Option as Meta key)。hivemux uninstall 会删除它。各终端具体会改动什么,见安装页面。

其他任何终端也都可以:在每个窗口中自己启动 tmux(或 tmux attach),并把 Option 键设为发送 Meta/Esc+,让 Option 快捷键生效。在不支持 OSC 777 的终端中,你将收不到桌面通知。

下一步:第一个项目请看 /zh/docs/quick-start/,所有快捷键请看 /zh/docs/keys/。

常见问题

在 Ghostty 中按 Option+1 没有反应,为什么?

Ghostty 必须把 Option 作为 Alt 发送。设置 macos-option-as-alt = true,然后完全退出 Ghostty 再重新打开;重新加载配置不会改变已经打开的窗口。

使用 hivemux 必须用 Ghostty 吗?

不必。只要你自己启动 tmux,任何终端都可以;hivemux terminal 也能让 iTerm2、WezTerm、kitty、Alacritty 或 Terminal 的每个窗口都在 tmux 中打开。之所以推荐 Ghostty,是因为 hivemux setup 可以安装它并让每个 Ghostty 窗口连接到 tmux,而且 Ghostty 能显示 hivemux 发送的终端通知。

如何在 Ghostty 中更改字体?

在 ~/.config/ghostty/config 中 hivemux 配置块之外添加 font-family 和 font-size 两行,然后按 Cmd+Shift+逗号 重新加载。ghostty +list-fonts 会列出已安装的字体名称。

关闭 Ghostty 会停止我的 Claude Code 会话吗?

当 Ghostty 连接到 tmux 时不会。进程归 tmux 管理,所以 Claude 会继续运行,下一个 Ghostty 窗口会连接到你上次查看的会话。