🔥

WezTermのタブにOSC 2でGit root名を表示する

に公開

WezTermのタブに作業ディレクトリを表示したかったのですが単にディレクトリ名だと嫌だったので、Gitで管理されているディレクトリならGitのroot名を、そうでなければカレントディレクトリ名を出力するようにしました。

wezterm + osc2

OSC 2というウィンドウのタイトルをセットするエスケープシーケンスがあるみたいなのでこれ使いました。
こんな感じのスクリプトです。

# タブにgit root名を設定
function __osc2_title() {
    local git_root=$(git rev-parse --show-toplevel 2>/dev/null)
    local title=${${git_root:t}:-${PWD:t}}
    printf '\033]2;%s\033\\' "$title"
}
chpwd_functions+=(__osc2_title)
__osc2_title  # 初回実行
local wezterm = require("wezterm")

local M = {}

function M.setup()
    wezterm.on("format-tab-title", function(tab)
        local title = tab.active_pane.title
        if title == "" then title = "-" end

        return {
            { Text = " " .. title .. " " },
        }
    end)
end

return M
GitHubで編集を提案

Discussion