DEV Community

loading...

Quickly Open a File in the Browser from WSL

wrightdotclick profile image Thomas Wright ・2 min read

Mac users have this magic convenience of typing open index.html into their terminal to open a file from their current directory in the browser. Those of us on WSL aren't so lucky. If you're lucky, the open command will open a windows directory. Cool, I guess? But not exactly super helpful. The internet abounds with recommendations --- from trying xdg-open, configuring your www-browser defaults, and setting your PATH environments to some even more convoluted methods. But the question remains: what's the easiest way to open an HTML file in the browser from WSL?

The closest thing I've found is to use the Windows Subsystem for Linux's explorer.exe command. Assuming you've set your browser environment variable in bash, then typing something lile explorer.exe index.html should open the file index.html in your default browser. If that works, congrags! That's the simplest version of Mac's open command. Well, sort of...

Typing explorer.exe is still a little bit of a handful, and I'm lazy. I mean open is four characters, and explorer.exe is twelve. That's three times as many characters. No thanks!

Wouldn't it be nice if we could do something simpler? How about see index.html? Fortunately, we can leverage the ability to create aliases in bash to make this dream a reality. Just drop the following three lines directly into your terminal:

export BROWSER='/mnt/c/Windows/explorer.exe'
echo "alias see='explorer.exe'" >> ~/.bashrc
source ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

You're done! No need to play around in your config files. 🙌 Next time you need to open that index.html file, all you need to do is type see index.html and watch the magic happen. And it's one fewer character than those Mac folks. Just imagine what you could do with that kind of time savings...

a bear is dancing happily

Discussion (0)

Forem Open with the Forem app