Parin's Space
返回文章列表

Ghostty 安裝與設定

· 閱讀時間 3 分鐘

換到 Ghostty 有一段時間了,整體體驗比之前用的終端機好很多。趁這次整理設定檔,把安裝步驟和一些踩過的坑記下來。

安裝

最直接的方式是去官網下載 .dmg,拖進 /Applications 就好。

懶得手動的話用 Homebrew:

brew install --cask ghostty

設定檔在哪

~/.config/ghostty/config

第一次用要自己建目錄:

mkdir -p ~/.config/ghostty
touch ~/.config/ghostty/config

我目前的設定

term = xterm-ghostty
shell-integration = zsh
theme = Adventure
shell-integration-features = cursor,sudo,title,ssh-terminfo,ssh-env
confirm-close-surface = false
copy-on-select = clipboard

幾個比較需要解釋的選項:

  • shell-integration-features — 這個最重要,後面會細說。ssh-terminfossh-env 解決了大部分 SSH 相容性問題。
  • copy-on-select = clipboard — 選取文字直接進剪貼簿,不用再按 cmd+c,習慣之後回不去。
  • confirm-close-surface = false — 關掉關閉視窗的確認彈窗,每次都要多按一下很煩。

SSH 踩坑記錄

這是我剛換 Ghostty 時遇到最大的問題。

Ghostty 預設把 $TERM 設成 xterm-ghostty,但大多數 remote 主機根本沒有對應的 terminfo。結果就是連上去之後,zsh-autosuggestions 的補全會出現,但按 Enter 執行的卻不是那個指令——很詭異,害我以為是 zsh 設定出了問題。

後來用 Terminal.app 連同一台機器,完全正常,才確定是 terminfo 的問題。

現在的解法:ssh-terminfo

Ghostty 1.2.0 加了 ssh-terminfo,這個功能會在 SSH 連線時自動偵測 remote 有沒有 ghostty terminfo,沒有的話就用 infocmp + tic 幫你安裝,還會在本機快取,下次就不用重裝。

設定方式就是在 shell-integration-features 裡加上 ssh-terminfo,ssh-env

shell-integration-features = cursor,sudo,title,ssh-terminfo,ssh-env

ssh-env 會在連線時自動傳遞 COLORTERMTERM_PROGRAM 等變數,如果 terminfo 安裝失敗,會 fallback 到 xterm-256color

前提是本機有 infocmp(macOS 內建有)、remote 有 tic(裝了 ncurses 就有)。

快取管理:

# 看哪些 hosts 已經快取過
ghostty +ssh-cache --list

# 清掉特定 host 的快取(下次連線會重新安裝)
ghostty +ssh-cache --delete <HOST>

手動推 terminfo

如果自動安裝沒效(舊版 Ghostty 或 remote 權限問題),可以手動推:

infocmp -x xterm-ghostty | ssh <HOST> -- tic -x -

有看到這個輸出就代表成功:

older tic versions may treat the description field as an alias

如果出現 alias ghostty multiply defined,代表已經裝過了,不用管它。

macOS Sonoma 以前要注意

系統內建的 ncurses 太舊,匯出的 terminfo 格式可能讓 remote 的 tic 讀不懂,會噴 Illegal character

解法是裝 Homebrew 版的 ncurses,然後用它的 infocmp

brew install ncurses
/opt/homebrew/opt/ncurses/bin/infocmp -x xterm-ghostty | ssh <HOST> -- tic -x -

實在不行就降級

如果 remote 不讓你裝 terminfo,最後手段是在 ~/.ssh/config 對該 host 強制降級 TERM

Host <hostname>
    SetEnv TERM=xterm-256color

確認有沒有裝成功

ssh <HOST> -- infocmp xterm-ghostty

有輸出 terminfo 內容就是好了。


其他常用設定

字型:

font-family = "JetBrains Mono"
font-size = 14

視窗留白和透明度:

window-padding-x = 10
window-padding-y = 10
background-opacity = 0.95

主題(內建很多,用 ghostty +list-themes 看):

theme = catppuccin-mocha

自訂快捷鍵:

keybind = ctrl+shift+t=new_tab
keybind = ctrl+shift+w=close_surface
keybind = ctrl+shift+n=new_window

常用快捷鍵速查

macOS 上 Ghostty 用 cmd 當主修飾鍵。

視窗 / 分頁

快捷鍵功能
cmd+n開新視窗
cmd+t開新分頁
cmd+w關閉目前 surface
cmd+shift+[ / ]切換分頁
cmd+1 ~ cmd+9跳到第 N 個分頁

Split

快捷鍵功能
cmd+d垂直分割
cmd+shift+d水平分割
cmd+opt+←/→/↑/↓切換 pane 焦點
cmd+ctrl+←/→/↑/↓調整 pane 大小

捲動 / 其他

快捷鍵功能
shift+pageup/down捲動一頁
cmd+k清除畫面
cmd+f搜尋
cmd+= / - / 0字型大小

完整動作清單見官方文件


Shell 整合手動載入

通常安裝後 zsh 會自動啟用,如果沒有,在 ~/.zshrc 加:

if [ "$TERM_PROGRAM" = "ghostty" ]; then
  source /Applications/Ghostty.app/Contents/Resources/ghostty/shell-integration/zsh/ghostty-integration
fi

幾個實用指令

ghostty +list-themes      # 列出所有內建主題
ghostty +list-fonts       # 列出可用字型
ghostty +validate-config  # 檢查設定檔語法

參考:ghostty.org/docs · 設定參考 · GitHub