112

I'm trying to create an alias that opens google chrome to localhost. Port 80 in this case.

I'd also really like to be able to be in any git directory and have it open that specific project in the browser, but I'm not sure if that's even possible.

  • How do I open google chrome from the terminal?
  • What alias could I use to open the current git project in the browser?

More Details:

  1. My localhost is set to port 80.
  2. I store my git repositories in ~/Sites/ - meaning if I wanted to view any project in the browser it would be found here: http://localhost/FILENAME

Thank You

4

11 Answers 11

150

From the macOS Terminal, use open with the -a flag and give the name of the app you want to open. In this case "Google Chrome". You may pass it a file or URL you want it to open with.

open -a "Google Chrome" index.html 
4
  • 6
    I just added alias chrome="open -a 'Google Chrome'" to my .zshrc file. Thank you! Commented Jan 4, 2020 at 1:44
  • 3
    how do you pass flags Commented Feb 12, 2021 at 20:21
  • 4
    @MuhammadUmer To pass flags e.g. --disable-web-security, use open -a "Google Chrome" --args --disable-web-security Commented Aug 8, 2022 at 8:29
  • If Chrome is your default browser, just open index.html works as well. Commented Aug 9, 2023 at 7:48
47

On Linux, just use this command in a terminal:

google-chrome
4
  • 2
    Worth noting that, unlike xdg-open (or open on macOS) for using the default browser, google-chrome runs synchronously, i.e., blocks the calling shell until the newly opened tab is closed; appending & makes it asynchronous.
    – mklement0
    Commented Jul 31, 2019 at 20:19
  • 1
    In what scenario did it run in sync? I wasn't able to reproduce the sync behaviour you talked about, @mklement0 , always works async to me, never waits for tab to close.
    – Íhor Mé
    Commented Aug 14, 2019 at 5:51
  • @ÍhorMé: Google Chrome 76.0.3809.87 on Ubuntu 18.04.2 LTS.
    – mklement0
    Commented Aug 15, 2019 at 3:51
  • I'm also on that. Actually, now I find I do get this exp with google-chrome http://example --no-sandbox. And when I run without --no-sandbox I don't, but I can't run without that one under root. Throws [32610:32610:0815/130649.955364:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
    – Íhor Mé
    Commented Aug 15, 2019 at 10:07
32

If you just want to open the Google Chrome from terminal instantly for once then open -a "Google Chrome" works fine from Mac Terminal.

If you want to use an alias to call Chrome from terminal then you need to edit the bash profile and add an alias on ~/.bash_profile or ~/.zshrc file.The steps are below :

  • Edit ~/.bash_profile or ~/.zshrc file and add the following line alias chrome="open -a 'Google Chrome'"
  • Save and close the file.
  • Logout and relaunch Terminal
  • Type chrome filename for opening a local file.
  • Type chrome url for opening url.
2
  • 1
    bash has a ~/.bashrc as well. I would not recommend aliases in ~/.profile, that is more about env variable.
    – Ulysse BN
    Commented Oct 26, 2018 at 8:22
  • What is the -a ? I can't find it in the docs. Commented Jan 11, 2023 at 2:35
14

UPDATE:

  1. How do I open google chrome from the terminal?

Thank you for the quick response. open http://localhost/ opened that domain in my default browser on my Mac.

  1. What alias could I use to open the current git project in the browser?

I ended up writing this alias, did the trick:

# Opens git file's localhost; ${PWD##*/} is the current directory's name
alias lcl='open "http://localhost/${PWD##*/}/"'

Thank you again!

1
9

For macOS you can use this command:

open -a Google\ Chrome "https://google.com"
5

just type

google-chrome

it works. Thanks.

3
  • 2
    does not work in macOS Catalina. However just using open <URL> works if chrome is your default browser. Commented Apr 7, 2020 at 23:00
  • actually, i do work in ubuntu, and its working fine. But am not sure in macOS. Commented Apr 8, 2020 at 5:37
  • Doesn't work on macOS Sonoma either
    – Nisim Naim
    Commented Feb 29 at 13:38
4

on mac terminal (at least in ZSH): open stackoverflow.com (opens site in new tab in your chrome default browser)

4

Use command

google-chrome-stable

We can also use command

google-chrome

To open terminal but in my case when I make an interrupt ctrl + c then it get closed so I would recommend to use google-chrome-stable instead of google-chrome

4

For Windows:

start chrome "https://www.google.com"
2

In Terminal, type open -a Google\ Chrome

This will open Google Chrome application without any need to manipulate directories!

2

On linux [ubuntu] in terminal, write:

google-chrome 'https://google.com' 2> /dev/null

ending part 2> /dev/null ommits output from command in terminal and allows to type without disturbing comments from that command.

for new-ones in scripting (linux users):
u can also create some nice readable function in ~/.bashrc directory like:

open(){
    google-chrome $1 2> /dev/null
}

and in terminal using it like this:

open htts://google.com

Not the answer you're looking for? Browse other questions tagged or ask your own question.