DEV Community 👩‍💻👨‍💻

TJ Wright
TJ Wright

Posted on

Quickly Open a File in the Browser from WSL

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

Top comments (2)

Collapse
 
hardikgangani profile image
Hardik-gangani • Edited on

On Windows Machine

1.Go to Git Bash or Command prompt on windows.
2.Go to your directory path where the index.html is stored.
3.Cd Your path till the directory where that index.html file is stored
4.Simple Type start index.html on your terminal of gitbash/command prompt
5.WOHOHOHOHO Finally opened the file.

Thanks,
Hardik Gangani

Collapse
 
christopherkrad profile image
Christopher Rad

This is helpful, but users should keep in mind the command will open up a directory if you're not in the same directory as the file you're looking for

For example: see docs/index.html will open up your file explorer and not index.html

Let's Get Wacky


Use any Linode offering to create something unique or silly in the DEV x Linode Hackathon 2022 and win the Wacky Wildcard category

Join the Hackathon <-