Shell 整合

Ghostty 的部分功能需要與你的 shell 整合。Ghostty 可以為 bash、elvish、fish、nushell 和 zsh 自動注入 shell 整合。

功能

  • 對游標位於提示符前的終端機,關閉時不再要求確認。
  • 新終端機以上一個取得焦點的終端機之工作目錄啟動。
  • 複雜的提示符透過允許 shell 重繪--而非重排--提示符列而正確調整大小。
  • 按住 Control(Linux)或 Command(macOS)的同時三按滑鼠,可以選取命令的輸出。
  • 提示符處的游標變成一條豎線,以表示常見的文字編輯。
  • jump_to_prompt 鍵位綁定可用於在提示符之間向後和向前滾動終端機視窗。
  • Alt+點按(macOS 上為 option+點按)在提示符處會將游標移動到點按位置。
  • sudo 可被自動包裹以保留 Ghostty terminfo(預設為停用)。
  • ssh 可被自動包裹以正確設定遠端終端機環境。詳見 SSH(預設為停用)。

Shell 整合注入

Ghostty 會為 bash、elvish、fish、nushell 和 zsh 自動注入 shell 整合程式碼。

Note

macOS 自帶的 Bash 版本(/bin/bash)不支援自動 shell 整合。你需要手動 source shell 整合腳本(如下所示)。你也可以從 Homebrew 或其他來源安裝標準版本的 Bash,並設為你的 shell。

其他 shell 沒有編寫 shell 整合程式碼,但它們在 Ghostty 中可以正常運作,只是上述 shell 整合功能可能部分或全部不可用。

Nushell 自身提供了這些功能中的許多(例如標題與游標處理),故其 Ghostty 整合側重於 Ghostty 特有的功能,如 sudo、ssh-env 和 ssh-terminfo。類似地,較新版本的 Fish(4.0+)內建支援提示符標記,因此 jump_to_prompt、提示符調整大小與提示符選擇無需 shell 整合即可運作。

Ghostty 透過對待執行命令的基名進行簡單字串匹配來偵測你的 shell。如果你使用的受支援 shell 名稱不同,可以設定 shell-integration 配置選項來強制啟用 shell 整合:

shell-integration = fish

如果想停用自動 shell 整合:

shell-integration = none

驗證 Shell 整合

要驗證 shell 整合是否正常運作,請尋找以下日誌行:

info(io_exec): using Ghostty resources dir from env var: /Applications/Ghostty.app/Contents/Resources
info(io_exec): shell integration automatically injected shell=termio.shell_integration.Shell.fish

若看到以下任意一條,代表某些地方不正確。最常見的原因是 GHOSTTY_RESOURCES_DIR 未指向正確的位置。

ghostty terminfo not found, using xterm-256color
shell could not be detected, no automatic shell integration will be injected

疑難排解

自動 shell 整合要正常運作,Ghostty 必須從 macOS 應用程式套件執行,或在 Ghostty 執行目錄的上層某處可以使用 zig-out/share 的內容。

在 Linux 上,如果從 zig-out 目錄樹結構(標準的 FHS 風格樹)執行,或透過 -p 從正確安裝的位置執行,這應該會自動生效(見 從源碼建置)。

你也可以手動設定 GHOSTTY_RESOURCES_DIR 指向 zig-out/share/ghostty 的內容。要驗證此目錄,檔案 $GHOSTTY_RESOURCES_DIR/../terminfo/ghostty.terminfo 應存在。不過,手動設定該環境變數通常表示還有別的問題,你應該進一步調查。

手動 Shell 整合設定

shell 整合也可以在你的 shell 設定中手動 source。這可確保 shell 整合在更多情境下運作(例如在 Ghostty 內切換 shell 時)。

Ghostty 啟動時會自動設定 GHOSTTY_RESOURCES_DIR 環境變數,所以你可以用它 (1) 偵測 shell 是否在 Ghostty 內啟動,(2) 尋找 shell 整合。

例如,對 bash 而言,你需要把這段放在 ~/.bashrc 的 頂部:

# Ghostty shell integration for Bash. This should be at the top of your bashrc!
if [ -n "${GHOSTTY_RESOURCES_DIR}" ]; then
    builtin source "${GHOSTTY_RESOURCES_DIR}/shell-integration/bash/ghostty.bash"
fi

Note

從技術上講,bash shell 整合腳本可以從任何地方 source,但某些 bash 設定可能與 shell 整合衝突。為避免這種情況,我們建議在 shell 設定中盡早 source 該腳本。

每個 shell 整合的安裝說明都直接記錄在內:

Shell整合
bash${GHOSTTY_RESOURCES_DIR}/shell-integration/bash/ghostty.bash
elvish${GHOSTTY_RESOURCES_DIR}/shell-integration/elvish/lib/ghostty-integration.elv
fish"$GHOSTTY_RESOURCES_DIR"/shell-integration/fish/vendor_conf.d/ghostty-shell-integration.fish
nushell$GHOSTTY_RESOURCES_DIR/shell-integration/nushell/vendor/autoload/ghostty.nu
zsh${GHOSTTY_RESOURCES_DIR}/shell-integration/zsh/ghostty-integration

有關各 shell 的細節,請見 shell-integration/README.md。

切換 Shell 與 Shell 整合

如上一節所述,自動 shell 整合僅對 Ghostty 啟動時的_最初啟動的 shell_ 有效。

如果你在 Ghostty 內切換 shell,例如手動執行 bash 或使用 nix-shell 這類命令,該 shell 中將_失去_ shell 整合(原本的 shell 程序中仍會繼續運作)。

要在這些情況下使用 shell 整合,你必須按照上一節所示,手動 source Ghostty 對應 shell 的程式碼。

SSH 整合

ssh-env 和 ssh-terminfo shell 整合功能會在你的互動式 shell 中包裹 ssh,使 Ghostty 的 +ssh CLI 動作被透明地呼叫。這會設定好遠端終端機環境,讓透過 ssh 執行的應用程式正常運作。

詳見 SSH,包括如何在互動式 shell 之外直接使用 +ssh。