Inside WSL2 opens a html file in the browser that is in Win10. The local file path is transformed between Linux and Win10 notation.
repository; version: 2021.823.702 date: 2021-08-23 authors: Luciano Bestia
Install it from crates.io and add 2 symbolic links and one env variable:
```bash cargo install wslopenbrowser sudo ln -sf "/mnt/c/Program Files/Mozilla Firefox/firefox.exe" /usr/bin/browserinwin export BROWSER='/usr/bin/wslopenbrowser'
www
to open a browserln -sf "/usr/bin/wslopenbrowser" www
cd ~/rustprojects/wslopenbrowser www docs/index.html
www docs
cd docs www .
www
www rust-lang.org ```
Congratulations! You have just opened a windows browser from WSL2.
Inside WSL2 I want to open a html file in the browser. But the browser is in Win10.
This is useful in Rust for the documentation:
bash
cargo doc --open
or for simply open a file in a browser from bash:
bash
www index.html
I prepared a symbolic link to the firefox exe.
```bash ln -s "/mnt/c/Program Files/Mozilla Firefox/firefox.exe" /usr/bin/browserinwin
browserinwin http://github.com
export BROWSER='/usr/bin/browserinwin'
xdg-open http://rust-lang.org ```
This works great for URL, but it does not work for local files, because Linux and Windows see the same file with different paths.
The command ln -sf
is permanent and persistent. It makes a file that stays there forever.
But export BROWSER=
is NOT persistent. You need to add this command to ~/.bashrc
that runs it on every start of terminal.
For example:
Linux: /home/luciano/index.html
Win10: \\wsl$\Debian\home\luciano\index.html
OR:
Linux: /mnt/c/Users/Luciano/Downloads\index.html
Win10: c:\Users\Luciano\Downloads\index.html
I need a way to transform the path prior to call the browser.
Let's make a Rust CLI for that.
This is a simple binary. For good habit I separated the lib from the bin.
Use cargo auto to run automation tasks: build, release, doc, copytousr_bin,....
After cargo auto copy_to_usr_bin
we can now open the browser like this:
bash
wsl_open_browser http://rust-lang.org
wsl_open_browser /home/luciano/index.html
wsl_open_browser /mnt/c/Users/Luciano/Downloads/index.html
wsl_open_browser docs/index.html
But I want to use xdg-open.
xdg-open is the "Open any file" for linux.
For that I need to set the environment variable BROWSER.
To make it persistent, copy this line to ~/.bashrc
.
bash
export BROWSER='/usr/bin/wsl_open_browser'
Finally we can open the browser like this:
bash
xdg-open http://rust-lang.org
xdg-open /home/luciano/index.html
xdg-open /mnt/c/Users/Luciano/Downloads/index.html
and the Rust documentation works fine:
bash
cargo doc --open
www docs
or www .
I am still not satisfied.
I want to open the browser from Debian bash terminal with this simple syntax:
bash
www docs
www .
www rust-lang.org
I will make another alias:
bash
ln -sf "/usr/bin/wsl_open_browser" www
The command ln -sf
is permanent and persistent. It makes a file that stays there forever.