👋

WezTermでファイルパスをクリックして直接VS Codeで開く方法

に公開

Claude Codeを使っていると、ファイルパスを表示してくれたりするんですが毎回パスを選択 → コピー → VS Codeで Ctrl + Pして該当ファイルまで飛ぶ〜 といった作業をしていてめんどくさいのでファイルパスをクリックしたら直接VS Codeで開けるようにしてみました。
なお vscode:// というディープリンクを使えばVS Codeで開けるんですが、これが絶対パスしか受け付けてくれないのでハンドラーをかましていい感じにパスを構築してます。

-- ファイルパスをVS Codeで開けるように
table.insert(config.hyperlink_rules, {
    regex = [[([\w/][\w/\.-]*\.\w+)(:\d+)?(:\d+)?]],
    format = "vscode://file$PWD/$1$2$3",
})

-- open-uri イベントハンドラー
-- $PWD を実際のカレントディレクトリに置換して VS Code で開く
wezterm.on("open-uri", function(window, pane, uri)
    local start = uri:find("$PWD", 1, true)
    if start then
        local cwd_uri = pane:get_current_working_dir()
        if cwd_uri and cwd_uri.file_path then
            uri = uri:gsub("$PWD", cwd_uri.file_path)
            wezterm.open_with(uri)
            return false
        end
    end
end)

参考

https://github.com/wezterm/wezterm/discussions/4157

GitHubで編集を提案

Discussion