(zsh/tmux/WSL) ターミナルの作業中ディレクトリにOSの「ファイルを開く」からもアクセスしたい
motemen さんの ターミナルの作業中ディレクトリにOSの「ファイルを開く」からもアクセスしたい を見ていいなと思ったので自分もやってみた。ただ、WSL 内で動く tmux の作業ディレクトリを Windows 側に同期したいので、若干ややこしいことをすることになる。
以下のように wsl-update-cwds をつくる。
#!/usr/bin/env ruby
is_wsl = ENV.has_key?('WSLENV')
if !is_wsl
exit 0
end
# PWSH = '/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe'
PWSH = '/mnt/c/Program Files/PowerShell/7/pwsh.exe'
dirs = `tmux list-windows -F '\#{pane_current_path}'`.split("\n").uniq
command = <<~CMD
$WshShell = New-Object -COMObject WScript.Shell
function Create-Shortcut($targetPath, $shortcutPath) {
$Shortcut = $WshShell.CreateShortcut($shortcutPath)
$Shortcut.TargetPath = $targetPath
$Shortcut.Save()
}
Remove-Item -Path $env:USERPROFILE/cwds/*.lnk
CMD
dirs.each do |dir|
basename = File.basename(dir)
win_path = `wslpath -aw '#{dir}'`.chomp
command << "Create-Shortcut '#{win_path}' $env:USERPROFILE/cwds/#{basename}.lnk\n"
end
# Since pwsh takes a bit of time to start up, run it asynchronously using spawn.
pid = spawn(PWSH, '-NoProfile', '-NoLogo', '-Command', command)
- tmux から作業ディレクトリをひっぱってくる
- wslpath コマンドで Windows 側のパスに変換する
- ↑の情報から PowerShell のコードを生成し、pwsh でショートカットとして生成する
なんでショートカットなのか? というと Junction だと UNC パスにリンクを貼れず、Symlink だと管理者権限が必要になって嫌なので、ほかに方法がない。
あとは zsh の chpwd フックで実行させる。WSLENV 環境変数を見て、存在しないなら実行しないように
chpwd () {
if [[ -n "${WSLENV+x}" ]]; then
# in WSL
$HOME/dotfiles/bin/wsl-update-cwds
fi
}
関連エントリー
- WSL2 で pbcopy/pbpaste pbcopy は簡単で、clip.exe を呼べばいいだけ。 pbpaste は一発でできる Windows コマンドがなぜかないので Po...
- tmux 用の cdd (ウィンドウ番号を指定して cd) GNU screen のとき定義していた cdd という、既に開いている別ウィンドウのディレクトリにcdするコマンドがあった(ref. cd...
- Error code: Wsl/Service/CreateInstance/MountVhd/E_ACCESSDENIED Windows を再インストールして、 wsl --import-in-place Ubuntu D:\wsl\ubuntu\ext4.vh...
- メインPCのWindows 移行 こまごま Windows Tips みたいのを書いてるけどメインPCを macOS から Windows に移す決心をしていろいろやってる。...
- WSL2 の .vhdx はどこ? dir "$env:LOCALAPPDATA\Packages\*UbuntuonWindows*\LocalState\*.vhdx" で...