以前、こんな記事WSL から openを書きました。
そしたら、今日上がったdotfiles Advent Calendar 2019 の 18 日目の記事WSLにもそこそこ使えるopenが欲しい!で、色々と改善する方法を教えてもらえたので早速取り入れたいと思います。
改善点は以下のとおりです。
1. ググった結果をopen
2. URLをopen
3. 複数入力に対応
注意
powershell.exe start
は
Import-Module : The specified module 'C:\tools\poshgit\dahlbyk-posh-git-4184928\src\posh-git.psd1' was not loaded because no valid module file was found in any module directory.
At C:\Users\username\OneDrive\写真\ドキュメント\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:2 char:1
+ Import-Module 'C:\tools\poshgit\dahlbyk-posh-git-4184928\src\posh-git ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (C:\tools\poshgi...c\posh-git.psd1:String) [Import-Module], FileNot FoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
と怒られたので使っていません。
実装
if[[$(uname-r)=~ Microsoft ]];then
local Chrome='/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe'function open(){if[$# -eq 0 ];then
echo"ERROR: get no input argument."echo"Please specify file-paths, URLs, googling-words"echo"open [file/path]"return 1
fi
for arg;do
if[-e"${arg}"];then
readlink-f${arg} | xargs wslpath -m | xargs cmd.exe /c start
elif[[${arg}=~ http ]];then
echo"${arg}" | xargs -I{} bash -c"${Chrome} '{}'"else
echo"${arg}" | sed's/ /+/g' | xargs -I{} bash -c"${Chrome} 'https://www.google.com/search?q={}'"fi
done}fi
実行例
open "hello world" https://www.google.co.jp https://qiita.com "wsl open" ~/Downloads
実行結果
- chrome(インターネット・ブラウザ)
- hello world の検索結果 (google.com)
- google.com
- qiita.com
- wsl open の検索結果 (google.com)
- explore(ファイル・ブラウザ)
- ダウンロード(シンボリックリンクを事前に設定している必要があります)
zsh の safix alias を使えば、pptやpdfも開けます。参考:WSL から open
以前との違い
以前詳細に記事を書いたので、以前と変えていない箇所については説明しません。参考:WSL から open
no input error
if[$# -eq 0 ];then
echo"ERROR: get no input argument."echo"Please specify file-paths, URLs, googling-words"echo"open [file-paths/URLs/googling-words]"return 1
fi
- 入力がない場合はエラーを返す
複数の入力に対応
for arg;do
if[-e"${arg}"];then
readlink-f${arg} | xargs wslpath -m | xargs cmd.exe /c start
fi
done
$1
を${arg}
に変えました。 参考:Bashでいろいろループするpath
があるとき(=-e "${arg}"
がTrue
)はファイルを開きます。 参考:WSLにもそこそこ使えるopenが欲しい!
Chrome で開く
local Chrome='/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe'for arg;do
if[[${arg}=~ http ]];then
echo"${arg}" | xargs -I{} bash -c"${Chrome} '{}'"else
echo"${arg}" | sed's/ /+/g' | xargs -I{} bash -c"${Chrome} 'https://www.google.com/search?q={}'"fi
done
http
が含まれるとき(=${arg} =~ http
がTrue
)URLだと考えて、Chromeで開きます。 参考:WSLにもそこそこ使えるopenが欲しい!arg
の replace フラッグ-I
で、文字列{}
を置換。bash -c
文字列をbash で実行 参考:shellの-cオプションについて...
- そうでない場合は Google で検索。 参考:WSLにもそこそこ使えるopenが欲しい!
おわりに
Chrome='/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe'
はブラウザであれば変えても多分大丈夫powershell.exe start
を使ったほうがいい理由と使えない理由が知りたい- wsl 内からフォルダを開こうとすると、怒られた(開けるけど)
'\\wsl$\Ubuntu-18.04\home\wsl_username'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.