103

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

mklement0
  • 382,024
  • 64
  • 607
  • 775
Spencer Rohan
  • 1,759
  • 3
  • 12
  • 23

11 Answers11

141

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 
mklement0
  • 382,024
  • 64
  • 607
  • 775
John
  • 1,823
  • 2
  • 13
  • 14
43

On Linux, just use this command in a terminal:

google-chrome
mklement0
  • 382,024
  • 64
  • 607
  • 775
Miko
  • 587
  • 4
  • 3
  • 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 Jul 31 '19 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é Aug 14 '19 at 05:51
  • @ÍhorMé: Google Chrome 76.0.3809.87 on Ubuntu 18.04.2 LTS. – mklement0 Aug 15 '19 at 03: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é Aug 15 '19 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.
Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
theRana
  • 704
  • 9
  • 10
13

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!

mklement0
  • 382,024
  • 64
  • 607
  • 775
Spencer Rohan
  • 1,759
  • 3
  • 12
  • 23
  • If someone else wants to use it inside a Makefile on linux, it worked for me when I used `xdg-open`. Found the tip here https://stackoverflow.com/a/35811643/1594227 – ricardorover Apr 02 '22 at 20:44
6

just type

google-chrome

it works. Thanks.

Y. Joy Ch. Singha
  • 3,056
  • 24
  • 26
6

For macOS you can use this command:

open -a Google\ Chrome "https://google.com"
аlex
  • 5,426
  • 1
  • 29
  • 38
4

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

MauiGonzo
  • 41
  • 3
3

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

dippas
  • 58,591
  • 15
  • 114
  • 126
Ajay kumar
  • 31
  • 2
2

In Terminal, type open -a Google\ Chrome

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

DGomonov
  • 715
  • 1
  • 7
  • 19
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
Paweł Witek
  • 171
  • 11
2

For Windows:

start chrome "https://www.google.com"
Rohim Chou
  • 907
  • 10
  • 16