Make iTerm Open Links in Chrome Instead of Safari
Tags: safari, chrome, iterm, links
Published: 2021-02-20 19:27:08

Do you prefer to do all your development work in Chrome but still want to have Safari as your default browser? Sick of having iTerm automatically open all links in Safari instead of Chrome, and then having to copy said link and paste it into Chrome yourself? That takes up so many valuable seconds!

Inspite of all my best research, I wasn’t able to find a way to configure this in iTerm’s preferences. Sure, there’s the Context Menu Items for Smart Selection Rule that’s mentioned in [this Stack Overflow answer](https://stackoverflow.com/questions/46568358/how-do-i-configure-iterm2-to-open-urls-in-a-specific-browser), but I wasn’t able to get that to do anything. Like, at all. But maybe I’m just stupid.

Lacking an official approach, I did stumble across this hack: Add `BROWSER=chrome` to your `.env` file. Going forward, whenever you `npm start`, it should open with Chrome (even if Safari, or whatever other app, is your default browser).

A few caveats:

1. You have to do this individually with every repo going forward. But, on the plus side, once you do it initially with that repo, you should never need to worry about it opening in Chrome.
2. React apps come with a lot of Node modules pre-installed. ENV is not one of them. You’ll need to `npm i dotenv` after you `npx create-react-app`. Then, of course, `touch .env` once you’re in the repo’s main folder, so the file will be created at the top level. Interestingly, after merely creating the file and storing the above string in it, you don’t need to do anything else. As in, you don’t need to `require('dotenv').config()` in your `index.js` file or anywhere else (which I had to do with my other Node projects). If these additional steps sound unappealing, you could also just run this one, slightly longer line every time you plan to do an `npm start`: just type `BROWSER=chrome npm start`, which achieves the same effect as the above configuration.
3. This approach wouldn’t work with any non-Node projects (React is Node compliant). So this wouldn’t work for any of vanilla JavaScript repos. Or anything in any other programming language. However, there may be some `.env` analog in those situations. Plus, if you’re opening a file from the terminal, you can always type `open -a chrome index.html` instead of `open index.html` (or whatever file) in order to make it open in Chrome. Not as simple, but it works.
4. Every OS handles this a little differently. Windows uses `chrome`, Linux uses `google-chrome`, and Mac uses `google chrome`. So depending on your computer, you may need to type something different after `BROWSER=` in your `.env` file. (I have a Mac, but `chrome` works for me; however, I think that’s because I renamed the Chrome app in my `Applications` folder from `Google Chrome` to just `Chrome` forever ago. TLDR: Try them out until you get a fit, check what your Chrome app is specifically named in your `Applications` folder if none of those are working, and try wrapping it in quotes if you need to use the two words `google chrome`.)

*h/t to the [Stack Overflow answer](https://stackoverflow.com/questions/51706882/create-react-app-how-do-i-npm-start-with-a-specific-browser) that finally led me to this approach*