0%

Ghostty + Claude Code 完全指南

为什么选择 Ghostty 作为 Claude Code 的终端?如何配置才能发挥最大效率?本文是一份从安装到精通的完整教程。

Mac 键盘符号对照 Command | Option | Control | Shift


为什么是 Ghostty

VSCode 的内置终端在长时间 Claude Code 会话中存在明显问题:

  • 内存泄漏:AI 产生的大量输出会导致终端卡顿甚至崩溃
  • 渲染瓶颈:集成终端的渲染性能不如原生终端
  • 快捷键冲突:编辑器快捷键与终端快捷键互相干扰

Ghostty 是更好的选择:

特性 Ghostty iTerm2 VSCode Terminal
GPU 加速渲染 Metal (macOS) / OpenGL Metal (macOS)
原生 Shift+Enter 无需配置 无需配置 需要配置
桌面通知 原生支持 原生支持 需要扩展
滚动缓冲区 可配置到 25MB+ 无限制 有限
长会话稳定性 极佳 良好 容易崩溃
分屏/标签页 原生支持 原生支持 依赖编辑器布局
Kitty 键盘协议 完整支持 不支持 不支持
配置方式 纯文本文件,可 Git 管理 GUI 偏好设置面板 JSON settings
资源占用 极低(Zig 编写) 中等(Objective-C) 高(Electron 宿主)
启动速度 极快 较快 依赖 VSCode 启动
开源协议 MIT GPLv2 MIT

Ghostty 是 Claude Code 官方文档中明确推荐的终端之一,与 iTerm2、WezTerm、Kitty 并列为一等公民。


Ghostty 基础配置

配置文件位于 ~/.config/ghostty/config,修改后按 ⌘+⇧+, 热重载。

字体与排版

1
2
3
4
font-family = "Maple Mono NF CN"
font-size = 14
font-thicken = true
adjust-cell-height = 2

推荐使用 Nerd Font 系列,确保图标和符号正常显示。adjust-cell-height = 2 增加行间距,长时间阅读 Claude 输出时更舒适。

主题与外观

1
2
3
4
5
6
7
8
theme = Catppuccin Mocha
background-opacity = 0.95
background-blur-radius = 30
macos-titlebar-style = transparent
window-padding-x = 10
window-padding-y = 8
window-save-state = always
window-theme = auto

如果你希望跟随系统自动切换亮暗主题:

1
theme = light:Catppuccin Latte,dark:Catppuccin Mocha

background-opacity = 0.95 配合 background-blur-radius = 30 提供微透明效果,既美观又能隐约看到后面的参考窗口。window-save-state = always 确保重启 Ghostty 后恢复之前的窗口布局。

光标与鼠标

1
2
3
4
5
cursor-style = bar
cursor-style-blink = true
cursor-opacity = 0.8
mouse-hide-while-typing = true
copy-on-select = clipboard

Shell 集成

1
shell-integration = detect

这是一个关键配置。Ghostty 会自动为 bash/zsh/fish 注入 shell 集成脚本,启用以下功能:

  • 智能窗口关闭:光标在提示符时关闭不需要确认
  • 工作目录追踪:新分屏/标签自动继承当前目录
  • 提示符导航:在历史输出之间快速跳转
  • 命令输出选择:三击 + ⌘ 选中整段命令输出

目录继承

1
window-inherit-working-directory = true

新建分屏和标签页时自动继承当前工作目录。在 Claude Code 的项目目录中开新分屏时,不需要再 cd 到项目目录。

Quick Terminal(Quake 模式)

1
2
3
4
quick-terminal-position = top
quick-terminal-screen = mouse
quick-terminal-autohide = true
quick-terminal-animation-duration = 0.15

全局快捷键 ⌃+` 随时呼出/隐藏一个下拉终端。适合快速执行命令后立即收起,不打断当前工作。

安全与剪贴板

1
2
3
clipboard-paste-protection = true
clipboard-paste-bracketed-safe = true
clipboard-trim-trailing-spaces = true

clipboard-paste-protection 在粘贴可能包含恶意命令的内容时弹出确认。clipboard-trim-trailing-spaces 自动清理复制内容末尾的空格。

性能

1
scrollback-limit = 25000000

25MB 的滚动缓冲区。Claude Code 长会话可能产生大量输出,这个值确保你不会丢失任何历史内容。Ghostty 对此采用惰性分配,不会在启动时就占用内存。


SAND 快捷键体系

来自 @dani_avila7 的助记法,用 S-A-N-D 四个字母记住所有窗口管理操作:

1
2
3
4
S = Split    (分屏)
A = Across (跨标签)
N = Navigate (导航)
D = Destroy (关闭)

S — Split 分屏

快捷键 操作
⌘+D 向右分屏
⌘+⇧+D 向下分屏

A — Across 标签页

快捷键 操作
⌘+T 新建标签页
⌘+⇧+Left 上一个标签
⌘+⇧+Right 下一个标签

N — Navigate 导航

快捷键 操作
⌘+⌥+Left 跳到左侧分屏
⌘+⌥+Right 跳到右侧分屏
⌘+⌥+Up 跳到上方分屏
⌘+⌥+Down 跳到下方分屏
⌘+⇧+E 均等分配所有分屏
⌘+⇧+F 放大/还原当前分屏

D — Destroy 关闭

快捷键 操作
⌘+W 关闭当前分屏/标签

对应的 Ghostty 配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# S — Split
keybind = cmd+d=new_split:right
keybind = cmd+shift+d=new_split:down

# A — Across
keybind = cmd+t=new_tab
keybind = cmd+shift+left=previous_tab
keybind = cmd+shift+right=next_tab

# N — Navigate
keybind = cmd+alt+left=goto_split:left
keybind = cmd+alt+right=goto_split:right
keybind = cmd+alt+up=goto_split:top
keybind = cmd+alt+down=goto_split:bottom
keybind = cmd+shift+e=equalize_splits
keybind = cmd+shift+f=toggle_split_zoom

# D — Destroy
keybind = cmd+w=close_surface

# Extras
keybind = cmd+plus=increase_font_size:1
keybind = cmd+minus=decrease_font_size:1
keybind = cmd+zero=reset_font_size
keybind = global:ctrl+grave_accent=toggle_quick_terminal
keybind = cmd+shift+comma=reload_config

Tip: 在显示器旁贴一张写着 SAND 的便签,几天后就能形成肌肉记忆。


Claude Code 核心快捷键

会话控制

快捷键 功能
⌃+C 取消当前输入或停止生成
Esc 中途停止 Claude 的操作
Esc Esc 打开回退菜单(rewind)
⌃+D 退出 Claude Code
⌃+L 清空输入框
⌃+R 搜索历史命令

模式切换

快捷键 功能
⇧+Tab 循环切换权限模式(Plan → Normal → Auto)
⌥+P 切换模型
⌥+T 切换扩展思考
⌥+O 切换 Fast 模式

输入技巧

快捷键 功能
⇧+Enter 换行(Ghostty 原生支持,无需配置)
\ + Enter 换行(备选方式)
⌃+J 换行(控制序列方式)
⌃+G 在外部编辑器中编写长提示
⌃+V / ⌘+V 粘贴图片

任务管理

快捷键 功能
⌃+B 将正在运行的命令移到后台
⌃+T 显示/隐藏任务列表
⌃+O 循环切换视图模式(默认 → Verbose → Focus)

文本编辑(Emacs 风格)

快捷键 功能
⌃+K 删除到行尾
⌃+U 删除到行首
⌃+Y 粘贴已删除的文本
⌥+B 后退一个词
⌥+F 前进一个词

快速命令前缀

前缀 功能
/ 斜杠命令和 Skills
! 直接执行 shell 命令
@ 文件路径自动补全

全屏渲染模式

这是 Claude Code 长会话体验的关键优化。在 ~/.zshrc 中添加:

1
export CLAUDE_CODE_NO_FLICKER=1

它做了什么

  • 使用终端的备用屏幕缓冲区(类似 vim/htop 的全屏模式)
  • 内存使用恒定:无论对话多长,只渲染可见内容
  • 输入框固定在底部,不会被输出推走
  • 启用鼠标支持:点击展开工具输出、点击 URL、拖选文字

全屏模式导航

快捷键 功能
PgUp / PgDn 翻页(半屏)
⌃+Home 跳到对话开头
⌃+End 跳到最新 + 恢复自动跟随
鼠标滚轮 逐行滚动

Ghostty 天然放大了鼠标滚轮事件,因此滚动速度比较自然,通常不需要额外调节 CLAUDE_CODE_SCROLL_SPEED

转录模式(⌃+O)

⌃+O 进入转录模式,提供类 Vim 的浏览体验:

快捷键 功能
/ 搜索对话内容
n / N 下一个/上一个匹配
j / k 上下滚动
g / G 跳到顶部/底部
[ 将对话写入终端滚动缓冲区(可用终端原生搜索)
v $EDITOR 中打开完整对话

Status Line 状态栏

Status Line 是 Claude Code 底部的自定义状态栏,实时显示会话数据。对于长会话来说至关重要。

快速配置

最简单的方式是在 Claude Code 中直接运行:

1
/statusline show model, context percentage with progress bar, git branch, and cost

手动配置

~/.claude/settings.json(全局用户设置,所有工作区生效)中添加:

1
2
3
4
5
6
7
{
"statusLine": {
"type": "command",
"command": "~/.claude/statusline.sh",
"refreshInterval": 10
}
}

然后创建脚本 ~/.claude/statusline.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
input=$(cat)

MODEL=$(echo "$input" | jq -r '.model.display_name // "?"')
DIR=$(echo "$input" | jq -r '.workspace.current_dir // "~"')
DIR_NAME="${DIR##*/}"
PCT=$(echo "$input" | jq -r '.context_window.used_percentage // 0' | cut -d. -f1)
COST=$(echo "$input" | jq -r '.cost.total_cost_usd // 0')

# Colors
GREEN='\033[32m'
YELLOW='\033[33m'
RED='\033[31m'
DIM='\033[2m'
RESET='\033[0m'

# Progress bar
BAR_WIDTH=10
FILLED=$((PCT * BAR_WIDTH / 100))
EMPTY=$((BAR_WIDTH - FILLED))
BAR=""
[ "$FILLED" -gt 0 ] && printf -v FILL "%${FILLED}s" && BAR="${FILL// /▓}"
[ "$EMPTY" -gt 0 ] && printf -v PAD "%${EMPTY}s" && BAR="${BAR}${PAD// /░}"

# Color by usage level
if [ "$PCT" -ge 80 ]; then
BAR_COLOR="$RED"
elif [ "$PCT" -ge 50 ]; then
BAR_COLOR="$YELLOW"
else
BAR_COLOR="$GREEN"
fi

# Git info
GIT_INFO=""
if git -C "$DIR" rev-parse --git-dir > /dev/null 2>&1; then
BRANCH=$(git -C "$DIR" branch --show-current 2>/dev/null)
STAGED=$(git -C "$DIR" diff --cached --numstat 2>/dev/null | wc -l | tr -d ' ')
MODIFIED=$(git -C "$DIR" diff --numstat 2>/dev/null | wc -l | tr -d ' ')
GIT_INFO=" ${DIM}|${RESET} ${BRANCH}"
[ "$STAGED" -gt 0 ] && GIT_INFO="${GIT_INFO} ${GREEN}+${STAGED}${RESET}"
[ "$MODIFIED" -gt 0 ] && GIT_INFO="${GIT_INFO} ${YELLOW}~${MODIFIED}${RESET}"
fi

COST_STR=$(printf '$%.2f' "$COST")

echo -e "[${MODEL}] ${DIR_NAME}${GIT_INFO} ${DIM}|${RESET} ${BAR_COLOR}${BAR} ${PCT}%${RESET} ${DIM}|${RESET} ${COST_STR}"
1
chmod +x ~/.claude/statusline.sh

可用数据字段

Status Line 脚本通过 stdin 接收 JSON 数据,常用字段:

字段 说明
model.display_name 当前模型名
workspace.current_dir 当前工作目录
context_window.used_percentage 上下文使用百分比
context_window.remaining_percentage 剩余百分比
cost.total_cost_usd 累计费用(美元)
cost.total_duration_ms 会话总时长
vim.mode Vim 模式状态(NORMAL/INSERT)
session_name 会话名称
rate_limits.five_hour.used_percentage 5 小时限额使用比

为什么 Status Line 很重要

context_window.used_percentage 超过 80% 时,Claude 的输出质量开始下降。状态栏让你始终掌握这个关键指标,在合适的时机执行 /compact/clear


分屏工作流

推荐布局:双栏模式

1
2
3
4
5
6
7
8
┌──────────────────────────┬─────────────────────┐
│ │ │
│ Claude Code │ Shell / Git │
│ (主工作区) │ (辅助操作) │
│ │ │
│ ⌘+D 创建右分屏 │ git diff / test │
│ │ │
└──────────────────────────┴─────────────────────┘

这是最常用的布局。左侧专注 Claude Code 对话,右侧随时执行辅助命令。

操作步骤:

  1. 打开 Ghostty,启动 Claude Code
  2. ⌘+D 向右分屏
  3. 右侧面板用于 git diffgit log、运行测试、查看文件
  4. ⌘+⌥+Left/Right 在两个面板之间跳转
  5. ⌘+⇧+F 临时放大某个面板(再按一次恢复)

进阶布局:三区模式

1
2
3
4
5
6
7
8
┌──────────────────────────┬─────────────────────┐
│ │ │
│ Claude Code │ Editor (vim/nvim) │
│ (主工作区) │ │
│ │ │
├──────────────────────────┴─────────────────────┤
│ 日志 / 测试 / 服务 │
└────────────────────────────────────────────────┘

操作步骤:

  1. 启动 Claude Code
  2. ⌘+D 右分屏 → 打开编辑器
  3. ⌘+⌥+Left 回到 Claude Code 面板
  4. ⌘+⇧+D 下分屏 → 运行开发服务器或测试
  5. ⌘+⇧+E 均等分配空间

多标签页工作流

用标签页管理不同项目或不同任务:

  • Tab 1:前端项目 Claude Code
  • Tab 2:后端项目 Claude Code
  • Tab 3:数据库/运维操作

⌘+T 新建标签,⌘+⇧+Left/Right 切换。


上下文窗口管理

这是使用 Claude Code 最重要的技能。上下文窗口填满后,性能显著下降。

上下文消耗来源

来源 大约 Token 数
系统提示 ~4,200
CLAUDE.md 取决于内容长度
环境信息 ~280
MCP 工具 ~120(按需加载)
每轮对话 数百到数千

关键命令

命令 用途
/clear 清空上下文,开始新对话
/compact 压缩上下文,保留关键信息
/compact 保留 API 变更列表 带指令的压缩
/cost 查看当前 token 使用量和费用

最佳实践

1. 任务间使用 /clear

不相关的任务不要在同一个会话中完成。每完成一个任务,/clear 一次。

2. 给 Claude 验证目标

这是最高杠杆的实践。告诉 Claude 如何验证:

1
修复 auth.ts 中的登录 bug。运行 npm test -- --grep "auth" 验证修复结果。

3. 先探索,再计划,最后编码

1
2
3
4
1. ⇧+Tab 进入 Plan Mode → 让 Claude 探索代码
2. 要求 Claude 给出实施计划
3. ⇧+Tab 切回 Normal Mode → 执行计划
4. 要求 Claude 提交并创建 PR

4. 用子代理隔离上下文

大量的文件读取、日志分析等操作会快速消耗上下文。使用子代理(Agent)可以让这些操作在独立上下文中执行,不污染主会话。

5. 避免常见陷阱

  • 厨房水槽式会话:什么都往一个会话里塞 → 及时 /clear
  • 反复纠正:两次纠正失败后,/clear 重写更好的提示
  • 过长的 CLAUDE.md:太长了 Claude 会忽略一半 → 精简到最关键的内容

Git Worktree 并行开发

这是进阶技巧:在多个 Ghostty 标签页中同时运行 Claude Code,各自独立工作。

原理

Git Worktree 允许同一个仓库在不同目录下同时检出不同分支,互不干扰。

操作流程

1
2
3
4
5
6
7
# 方式 1: 使用 Claude Code 内置 worktree 支持
claude -w feature-auth # 自动创建 worktree 并启动

# 方式 2: 手动创建
git worktree add ../my-project-feature-auth feature-auth
cd ../my-project-feature-auth
claude

Ghostty 中的并行布局

1
2
3
4
5
6
┌─ Tab 1: feature-auth ──────┐ ┌─ Tab 2: fix-bug-123 ────────┐
│ │ │ │
│ Claude Code │ │ Claude Code │
│ (在 worktree-auth 目录) │ │ (在 worktree-bug 目录) │
│ │ │ │
└─────────────────────────────┘ └──────────────────────────────┘

每个标签页是完全独立的 Claude Code 会话,有自己的上下文、分支和工作目录。适合同时推进多个 feature 或 bug fix。


会话管理技巧

命名会话

1
claude -n "oauth-migration"      # 启动命名会话

命名会话可以在后续恢复,方便长期项目:

1
2
claude -r "oauth-migration"      # 恢复命名会话
claude -r "oauth-migration" "继续实现 token 刷新逻辑"

恢复上一次会话

1
2
claude --continue    # 恢复最近一次对话
claude --resume # 从列表中选择

非交互模式

适合脚本化和批处理:

1
2
3
4
5
6
7
8
9
# 单次提问
claude -p "解释这个项目的架构"

# JSON 输出
claude -p "列出所有 API 端点" --output-format json

# 管道输入
tail -200 app.log | claude -p "总结错误信息"
git diff main --name-only | claude -p "检查安全问题"

批量处理

1
2
3
4
for file in $(cat files.txt); do
claude -p "将 $file 从 Class 组件迁移为 Hooks。返回 OK 或 FAIL。" \
--allowedTools "Edit,Bash(git commit *)"
done

后台命令

在 Claude Code 内部,当一个 Bash 命令正在执行时,按 ⌃+B 将其移到后台。你可以继续对话,命令完成后会收到通知。

Shell 命令快捷执行

在 Claude Code 提示符中直接用 ! 前缀执行 shell 命令:

1
2
3
! npm test
! git status
! cat src/auth.ts | head -50

Vim 模式

Claude Code 内置了完整的 Vim 编辑支持。

启用方式

在 Claude Code 中运行 /config,选择 Editor mode 设为 Vim。或者手动编辑 ~/.claude.json

1
2
3
{
"editorMode": "vim"
}

支持的操作

  • 移动h/j/k/lw/e/b0/$gg/Gf/F/t/T
  • 编辑d/c/y 配合文本对象 (iw, aw, i", a( 等)
  • 模式切换Esci/Ia/Ao/O

在 Status Line 中显示 Vim 模式

启用 Vim 模式后,Status Line 脚本可以读取 vim.mode 字段(值为 NORMALINSERT),在状态栏中显示当前模式。


CLAUDE.md 项目记忆

CLAUDE.md 是 Claude Code 的项目级配置文件,定义项目上下文和工作规范。

文件优先级

位置 用途 是否提交到 Git
~/.claude/CLAUDE.md 全局配置,所有项目生效 N/A
./CLAUDE.md 项目配置,团队共享
./CLAUDE.local.md 个人项目配置 否(gitignore)

快速生成

1
/init

Claude 会分析你的代码库并生成初始 CLAUDE.md。

引用其他文件

1
2
3
项目概述见 @README.md
Git 工作流程见 @docs/git-workflow.md
个人偏好见 @~/.claude/my-preferences.md

编写原则

  • 只写 Claude 无法从代码中推断的信息
  • 保持精简,过长的 CLAUDE.md 反而降低效果
  • 像维护代码一样定期审查和清理
  • 优先写规范和约束,而不是教程

tmux 集成

如果你在 Ghostty 中使用 tmux,需要额外配置以确保 Claude Code 的功能正常工作。

必要的 tmux 配置

~/.tmux.conf 中添加:

1
2
3
4
5
6
7
8
9
# 允许终端事件穿透 tmux(通知、进度条)
set -g allow-passthrough on

# 启用鼠标支持(全屏渲染需要)
set -g mouse on

# 扩展键支持(⇧+Enter 等)
set -s extended-keys on
set -as terminal-features 'xterm*:extkeys'

allow-passthrough 让 Claude Code 的桌面通知和进度条能穿透 tmux 到达 Ghostty。


性能调优与故障排除

诊断命令

在 Claude Code 中运行 /doctor,它会检查:安装状态、配置、MCP 服务器、快捷键绑定、上下文警告。

常见问题

闪烁/跳动

1
export CLAUDE_CODE_NO_FLICKER=1  # 加到 ~/.zshrc

CPU/内存占用高

  • 定期使用 /compact 压缩上下文
  • 任务间 /clear
  • 确保构建目录在 .gitignore

搜索性能慢

安装系统级 ripgrep:

1
2
brew install ripgrep
export USE_BUILTIN_RIPGREP=0 # 加到 ~/.zshrc

大量文本输入

不要直接粘贴大段内容到 Claude Code。写入文件后让 Claude 读取:

1
请读取 /tmp/long-input.txt 并分析其中的错误

Ghostty 配置热重载

修改配置后按 ⌘+⇧+, 即时生效,无需重启终端。


完整配置参考

~/.config/ghostty/config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# ============================================
# Ghostty Terminal - Claude Code Optimized
# ============================================
# Reload: ⌘+⇧+, (macOS)

# --- Typography ---
font-family = "Maple Mono NF CN"
font-size = 14
font-thicken = true
adjust-cell-height = 2

# --- Theme ---
theme = Catppuccin Mocha

# --- Window ---
background-opacity = 0.95
background-blur-radius = 30
macos-titlebar-style = transparent
window-padding-x = 10
window-padding-y = 8
window-save-state = always
window-theme = auto

# --- Cursor ---
cursor-style = bar
cursor-style-blink = true
cursor-opacity = 0.8

# --- Mouse ---
mouse-hide-while-typing = true
copy-on-select = clipboard

# --- Quick Terminal ---
quick-terminal-position = top
quick-terminal-screen = mouse
quick-terminal-autohide = true
quick-terminal-animation-duration = 0.15

# --- Security & Clipboard ---
clipboard-paste-protection = true
clipboard-paste-bracketed-safe = true
clipboard-trim-trailing-spaces = true

# --- Shell Integration ---
shell-integration = detect
window-inherit-working-directory = true

# --- SAND Keybindings ---
# S = Split
keybind = cmd+d=new_split:right
keybind = cmd+shift+d=new_split:down
# A = Across
keybind = cmd+t=new_tab
keybind = cmd+shift+left=previous_tab
keybind = cmd+shift+right=next_tab
# N = Navigate
keybind = cmd+alt+left=goto_split:left
keybind = cmd+alt+right=goto_split:right
keybind = cmd+alt+up=goto_split:top
keybind = cmd+alt+down=goto_split:bottom
keybind = cmd+shift+e=equalize_splits
keybind = cmd+shift+f=toggle_split_zoom
# D = Destroy
keybind = cmd+w=close_surface
# Extras
keybind = cmd+plus=increase_font_size:1
keybind = cmd+minus=decrease_font_size:1
keybind = cmd+zero=reset_font_size
keybind = global:ctrl+grave_accent=toggle_quick_terminal
keybind = cmd+shift+comma=reload_config

# --- Performance ---
scrollback-limit = 25000000

~/.zshrc 中需要添加的环境变量

1
2
# Claude Code: 全屏无闪烁渲染
export CLAUDE_CODE_NO_FLICKER=1

快速开始检查清单

  1. 安装 Ghostty 并应用上述配置
  2. ~/.zshrc 中添加 export CLAUDE_CODE_NO_FLICKER=1
  3. 安装 jq(brew install jq,Status Line 脚本依赖)
  4. 安装 ripgrep(brew install ripgrep,提升搜索性能)
  5. 安装 gh CLI(brew install gh,GitHub 集成)
  6. 在项目中运行 claude,然后 /init 生成 CLAUDE.md
  7. 运行 /statusline 配置状态栏
  8. 练习 SAND 快捷键
  9. 建立分屏工作流习惯

参考来源: