Ghostty 配置笔记

配置文件位置

macOS 上 Ghostty 按顺序读取以下配置文件:

~/.config/ghostty/config
~/Library/Application Support/com.mitchellh.ghostty/config

注意文件名是 config,不带扩展名。配置格式是简单的 key = value,一行一个,改完在 Ghostty 里按 ⌘⇧, 重载即可生效。

查看所有配置项及默认值(带文档注释):

ghostty +show-config --default --docs | less

从 Terminal.app 迁移

macOS Terminal.app 的配置可以导出为 .terminal 文件(本质是 plist)。以自带的 Basic 主题为例:

<key>Font</key>  <!-- NSKeyedArchiver 编码的 NSFont,解出来是 SF Mono Regular 16pt -->
<key>columnCount</key>
<integer>108</integer>
<key>rowCount</key>
<integer>35</integer>

Font 字段是 base64 的 NSKeyedArchiver 二进制 plist,可以用 Python 解:

import plistlib
d = plistlib.load(open('Basic.terminal', 'rb'))
font = plistlib.loads(d['Font'])
print(font['$objects'][1]['NSSize'], font['$objects'][2])
# 16.0 SFMono-Regular

Basic 主题没有显式定义颜色,即默认的白底黑字。对应的 Ghostty 配置:

font-family = SF Mono
font-style = Regular
font-size = 16
font-thicken = true

adjust-cell-height = 5%

background = #F5F1E8
foreground = #000000
bold-color = #000000

cursor-color = #000000
cursor-style = block

window-width = 108
window-height = 35

自定义配置

font-family = SF Mono
font-family-bold = JetBrains Mono Bold
font-family-italic = JetBrains Mono Italic
font-family-bold-italic = JetBrains Mono Bold Italic
font-style = Regular
font-size = 17
font-thicken = true

# 行间距加大一点
adjust-cell-height = 5%

background = #FAF9F6
foreground = #000000
bold-color = #000000

cursor-color = #000000
cursor-style = block

window-width = 108
window-height = 35

# ssh的会话使用传统term
term = xterm-256color

# 白底下加深绿色,避免命令/路径的绿色太淡
palette = 2=#1A7F37
palette = 10=#1A7F37

常用配置项

Ghostty 有 200+ 配置项,按用途分类记录一些常用的。

字体与外观

font-family = SF Mono            # 主字体
font-family-bold = ...           # 粗体单独指定
font-style = SemiBold            # 字重:Regular < Medium < SemiBold < Bold
font-size = 16
font-feature = liga              # 启用连字等 OpenType 特性
font-thicken = true              # macOS 字体加粗渲染,细字体更清晰

theme = iTerm2 Default           # 内置主题,几百套可选
background-opacity = 0.9         # 半透明背景
background-blur = true           # 毛玻璃效果
minimum-contrast = 1.1           # 自动保证文字对比度

palette = 0=#000000              # 自定义 16 色 ANSI 调色板,0-15
selection-background = #B3D7FF   # 选区颜色

窗口

window-width = 108               # 初始列数
window-height = 35               # 初始行数
window-padding-x = 4             # 内边距
window-padding-balance = true    # 边距均分
window-decoration = true         # 标题栏
macos-titlebar-style = transparent   # native / transparent / tabs
macos-option-as-alt = true       # Option 当 Alt 用,终端快捷键必备
window-save-state = always       # 退出时保存窗口状态
confirm-close-surface = false    # 关闭时不弹确认

光标与鼠标

cursor-style = block             # block / bar / underline
cursor-style-blink = false
cursor-click-to-move = true      # 点击移动光标
mouse-hide-while-typing = true
copy-on-select = true            # 选中即复制

滚动与行为

scrollback-limit = 100000        # 回滚行数
working-directory = home         # 启动目录
shell-integration = detect       # shell 集成(命令跳转、sudo 提示等)
notify-on-command-finish = true  # 长命令跑完发通知

快捷键

keybind = cmd+k=clear_screen
keybind = cmd+d=new_split:right
keybind = cmd+shift+d=new_split:down

分屏与标签

split-divider-color = #DDDDDD
unfocused-split-opacity = 0.9
window-show-tab-bar = true
window-new-tab-position = end

SSH 断连后按键乱码

现象:SSH 会话 idle 被断开后,回到本地 shell,按 Ctrl+C 或方向键 会显示成乱码(类似 ^[[1;5A 这样的转义序列被直接回显出来)。

原因:Ghostty 支持 kitty keyboard protocol(增强键盘协议)。远端的 TUI 程序(vim、Claude Code 等)启用该协议后,方向键、Ctrl 组合键会改发增强型转义序列。SSH 被 NAT/防火墙静默切断时,远端程序来不及发送“退出协议”的序列就死了,Ghostty 仍停留在增强模式;本地 shell 不认识这些序列,就当普通字符显示出来。

恢复方法(任选一):

  • 盲敲 reset 回车,完全复位终端;
  • 绑定 Ghostty 内置的 reset action(等价于 reset 命令):
keybind = ctrl+shift+r=reset

根治:避免 idle 断连,在 ~/.ssh/config 末尾加保活(详见《SSH 配置与端口转发》):

Host *
  ServerAliveInterval 60
  ServerAliveCountMax 3

连接不被静默切断,远端程序就能正常退出并恢复终端状态,乱码自然不会出现。

验证配置

# 查看当前生效的配置
ghostty +show-config

# 列出所有可选项
ghostty +show-config --default

官方文档:https://ghostty.org/docs/config