LoginSignup
4
4

More than 3 years have passed since last update.

[WSL]macOSのopenのようなものとエクスプローラのディレクトリにcdするコマンド

Last updated at Posted at 2019-10-24

2019/11/03 実装を大きく変更したので記事の内容を大幅に変更しました。
2019/11/10 さらに機能強化しました。
2020/01/13 検索機能に対応しました。

こちらのネタの Windows 版です。

たまたま見つけまして、これ WSL でも使えたら便利じゃね?ってことで作ってみました。 → eopen-ecd
WSL ターミナルがメインですが PowerShell と コマンドプロンプトでも使用できます。

fcd のクローンから初めたプロジェクトだったのですが、macOSの open コマンド相当の機能をオマケで追加したら、そちらのほうが機能が大きくなったのでメインの機能を eopen として ecd も付属するという形になっています。しましたが、ecdも機能強化したので合わせて一つのプロジェクトです。

使用例

eopen

macOS の open コマンド相当のもので、ターミナルからエクスプローラを起動します。ファイルや URI 指定した場合は関連付けされたアプリが起動します。(PowerShell 版とコマンドプロンプト版は未実装のため、explorer.exe を代わりに使用してください。大体の機能は一緒なので別にそれほど困らないはず・・・。)

# Open directory with (latest used) Explorer
eopen ~/.config/

# Open directory with new instance of Explorer
eopen -n ~/.config/

# Opens with Windows default application
eopen image.png

# Opens with Windows text editor
eopen -e ~/.bashrc

# Use sudo to edit the unowned file
eopen -e --sudo /etc/hosts

# Opens with Windows default browser
eopen http://google.com

# Open files and directories under Windows
eopen C:/Windows

# Open files and directories under Network shared folder
eopen //server/shared

# Others
eopen mailto:user@example.com   # Mail protocol
eopen calculator:               # Application
eopen shell:Personal            # Shell commands
eopen :MyComputerFolder         # Shorthand for shell:
eopen shell:::{2559a1f8-21d7-11d4-bdaf-00c04f60b9f0} # CLSID
eopen :                         # Current Explorer location
eopen :/workspace               # Relative path from current Explorer location

eclose

(最後に操作した)エクスプローラをターミナルから終了します。

ewd

(最後に操作した)エクスプローラの現在のディレクトリをターミナルに出力します。

ecd

エクスプローラの現在のディレクトリにターミナルのカレントディレクトリを変更します。fcd のクローンです。
ターミナルのディレクトリ変更と同時にエクスプローラのディレクトリも変更します。書式は変わりましたが、ecd : で以前と同じくエクスプローラの現在のディレクトリに移動することもできます。

# Change the directory to the '/etc'. (Both Terminal and Explorer)
ecd /etc

# Change the directory to the 'C:\Windows'. (Both Terminal and Explorer)
ecd c:/windows

# Change the directory to the current Explorer location.
ecd :

# Change the directory to the 'workspace' from current Explorer location.
ecd :/workspace

# Change the directory to the home directory.
ecd

# Change the directory to the Windows home (%USERPROFILE%) directory. (WSL only)
ecd ~~

# Change the directory to the ~~ directory instead Windows home directory.
ecd ./~~

epushd / epopd

cd の代わりに pushd を使用して移動します。 epopd で戻ることができます。

導入方法

リリースページ から適切な zip ファイルを適当なディレクトリに展開し、シェルのプロファイルに設定すれば使えます。例えば bash の場合は以下の一行を .bashrc に追加します。(パスは展開したディレクトリに変更してください。)

eval "$(sh "/path/to/eopen-ecd/init.sh")"

bash 以外にも zsh, ksh, mksh, yash, tcsh, fish に対応しています。さらに PowerShell と コマンドプロンプトにも対応しています。それぞれの導入方法はプロジェクトページを参照してください。

さいごに

初期バージョンはカジュアルな感じでメイン機能を PowerShell スクリプトで実装していたのですが、コマンド実行に時間がかかり反応速度が悪いので VC++ でガッツリと再実装してしまいました。これにより 400ms 以上かかっていたのが 100ms 程度と高速化しコマンド打ってから反応が返ってくるまでワンテンポあったのが即座に実行してるように感じられるようになりました。

eopen の類似のツールとしては wsl-open 等が有名だと思うのですが、eopen はWindows 10 1903 以降の //wsl$/ で始まるパスでも問題なく動作するので Linux 内のファイルも表示したり権限があれば編集できます。sudo 権限が必要なファイルの編集にも対応しており、/etc 以下のファイルを Windows のテキストエディタで修正することもできます。(一時的に /tmp 以下にユーザー権限でコピーして sudo を使って書き戻しています。)

類似の記事

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4