LoginSignup
9
3

More than 5 years have passed since last update.

Bash on Ubuntu on WindowsからhtmlファイルをWindowsのブラウザで開く

Last updated at Posted at 2017-08-23

素人の備忘録。
ガチの素人が試行錯誤しただけなので変なところだらけだと思います。

動機

VimでMarkdownのプレビューにPrevimを使っている。
普通なら、:PrevimOpenで編集中のMarkdownのプレビューがブラウザで開く。
Bash on Ubuntu on WindowsでもVimを使えるようにしたが、Previmがうまくいかない。

前提

wsl-terminalを使用しているので、cbwinが使える。
参考:
Bash on Ubuntu on Windows + オールインワンmintty
Bash on Ubuntu on Windowsからcmd / startを使う

wstartを使えば、UbuntuからWindowsのhtmlファイルをWindowsのブラウザで開くことはできる。

# cd /mnt/c/Users/user/Documents
# wstart test.html

ところが、Ubuntuの$HOME以下にあるhtmlファイルをWindowsのブラウザで開くことができない。

# cd ~/Documents
# wstart test.html

The system cannot find the file test.html

Previmはプレビュー用のhtmlファイルを作成し、g:previm_open_cmdで指定したコマンドで開いているらしい。
wstartを指定すればいいかと安易に考えていたが、上記のとおり開くことができない。

解決策

試行錯誤してみたところ、windowsのフルパスをwstartすれば、Windowsのブラウザで開けることがわかった。

# wstart C:/Users/user/AppData/Local/lxss/home/user/Documents/test.html

つまり、引数のファイルについてWindowsのフルパスを取得しwstartするシェルスクリプトを作り、それをg:previm_open_cmdに指定すればいいはず。
ということで~/mytoolsに以下のシェルスクリプトを作成。
参考:bash で ファイルの絶対パスを得る

~/mytool/startwinpath.sh
#!/bin/bash
windir='C:/Users/user/AppData/Local/lxss'
BoWpath=$(readlink -f $1)
wstart $windir$BoWpath

作成したシェルスクリプトに実行権限を与える。
参考:○○.shを./○○.shで実行できるようにする

# sudo chmod +x startwinpath.sh 

~/mytoolにパスを通す。

.zprofile
export PATH=$PATH:$HOME/mytool

g:previm_open_cmdを設定(ついでにリアルタイムプレビューも設定)。

.vimrc
" htmlを開くコマンドを指定
let g:previm_open_cmd="startwinpath.sh"
" リアルタイムプレビュー有効
let g:previm_enable_realtime=1

無事:PrevimOpenでプレビューがWindowsのブラウザで開いた。
リアルタイムプレビューもうまく動いているようだ。

どうせ素人なのでWindowsのVimを使えばいいのではないかという気がしなくもないが…

9
3
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
9
3