12

I want to make a command like:

chrome "site.com"

which will make a google chrome window to pop up with the site instantly. Is there any way to achieve this?

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • I don't have Chrome on my laptop here, but doing /c/Program\ Files\ \(x86\)/Mozilla\ Firefox/firefox.exe www.google.com brings up google ... in other words, you need to put the full path to the exe, and assuming Chrome works similar to firefox, you should be good – Foon Mar 01 '17 at 03:00
  • @Kotori My answer can be marked as correct answer which may help others too. – vijayinani Mar 01 '17 at 08:42

7 Answers7

40

This worked for me on Windows 10 and using Git Bash.

start chrome www.google.com
vijayinani
  • 2,548
  • 2
  • 26
  • 48
12
 start http://example.com

Opens the default browser with the URL.

Tested on Git Bash/Windows 10.

kost
  • 705
  • 7
  • 18
3

You can use explorer.exe that can open URL in default browser:

explorer.exe "https://www.google.com/"

Therefore there is a weird bug: URL should not contain ?, so:

explorer.exe "https://www.google.com/search?q=foo+bar"

will fail, and you need to use:

explorer.exe "https://www.google.com/search?q=foo+bar&\""

to work around.

By the way, I created bash function to open up Google page:

urlencode ()
{
    echo $1 | sed -e 's:%:%25:g' -e 's: :%20:g' -e 's:<:%3C:g' -e 's:\[:%5B:g' \
                  -e 's:>:%3E:g' -e 's:#:%23:g' -e 's:{:%7B:g' -e 's:\*:%2A:g' \
                  -e 's:}:%7D:g' -e 's:|:%7C:g' -e 's:+:%2B:g' -e 's:\\:%5C:g' \
                  -e 's:/:%2F:g' -e 's:?:%3F:g' -e 's^:^%3A^g' -e 's:\!:%21:g' \
                  -e 's:@:%40:g' -e 's:=:%3D:g' -e 's:&:%26:g' -e 's:\$:%24:g' \
                  -e 's:;:%3B:g' -e 's:~:%7E:g' -e 's:`:%60:g' -e 's:\^:%5E:g' -e 's:\]:%5D:g' 
}

google ()
{
    local a="$(urlencode "$(echo "$@")")"; a="${a// /+}"; 
    explorer.exe "https://www.google.com/search?q=${a// /+}&\""
}

alias ggle='google'
alias g='google'

You can use it as follows:

 g site:cppreference.com c++ empty string view

I find it quite useful, especially while coding ;)

Tested on Bash on Windows (Ubuntu 16.04.3 LTS; GNU bash, wersja 4.3.48).

AgainPsychoX
  • 1,527
  • 1
  • 16
  • 20
  • Quick question – is it correct that the quotes are not balanced with your escaping in the "you need to use" line? Should there not be an additional escaped quote? [Slightly related thought. It may also work if you use `%3F` instead of a literal question mark.] – Jongware Dec 31 '17 at 21:19
1

Here's what I'm using to open a URL or a file with Google Chrome into incognito mode from Git bash (provided with Git for Windows):

1. Open System Variables

  • Hit Windows Key+R at the same time to get command prompt.
  • Then type sysdm.cpl and hit the Enter key.

run sysdm.cpl

  • Go to Advanced1 and select Environmental Variables2
  • Select Path3 from System variables and click on Edit button4.

Windows - Environment variables

Keep this window opened (while get the chrome.exe folder path).

Windows - Edit environment variable

2. Get the chrome.exe folder path

Right click (or left if you have your mouse as left-handed) the Chrome icon5 (the one you use to open it) and select Properties7. Usually this method opens more Chrome actions so locate Google Chrome option6 and right-click on it and click on Properties7.

Google Chrome shortcut - Properties

On the Shortcut8 tab select the folder path from Start in:9 (Commonly %programfiles(x86)%\Google\Chrome\Application)

Google Chrome shortcut - Folder path

3. Adding Google Chrome folder path to Environment variables

Click on the New button10 from previously opened Edit environment variable window and paste the Google Chrome folder path into the new added line11 (delete the double quotes if exist at the beginning or the end of the path) and click on all the OK buttons from the opened windows.

New environment variable

4. Add custom command to bash

Open the Git bash terminal and edit (or create) the .bashrc file with your prefered text editor (Visual Studio code in this example)

code ~/.bashrc

Git bash - code .bashrc

Append the code from below. You can rename the function name (chromeIt in this example) to your needs.

# Open google Chrome
function chromeIt {
    if [ -z "$1" ]; then
        # display usage if no parameters given
        echo "Usage: chromeIt <file_name>|<url>"
    else
        if [ -f "$1" ] ; then
            chrome --incognito $(pwd)/$1
        else
            chrome --incognito $1
        fi
    fi
}

If you don't want incognito mode remove the --incognito parameter.

Very important note: Verify that your line ending is set as UNIX (LF)12. If you don't know how to do it search into google.

Visual Studio code - Edit or create .bashrc

Save the .bashrc file and reload it (using the Git bash terminal):

source ~/.bashrc

Git bash - source .bashrc

or you can use the shorter version of the command:

. ~/.bashrc

Git bash - source .bashrc shorter version

Close the Git bash terminal window or enter exit and re-open it.

Try to open a URL:

chromeIt google.com

Git bash - chromeIt open url

or a file:

chromeIt index.html

Git bash - chromeIt open file

I hope it works for you.

Community
  • 1
  • 1
quantme
  • 3,609
  • 4
  • 34
  • 49
1

Depending on your situation, a more cross-platform solution might be to use git web--browse, which will attempt to open a url (or file) in a browser.

For example, this would open a link to github for opening a PR:

URL="${GIT_REPO}/compare/${CURRENT_BRANCH}?expand=1"
echo "opening $URL"
git web--browse $URL

There is also a --browser option where you can target a specific browser.

See: https://git-scm.com/docs/git-web--browse

broc.seib
  • 21,643
  • 8
  • 63
  • 62
  • Great answer. This works out of the box with Git Bash, no extra setup needed. And it can open local files too `git web--browse my-local-file.html` – wisbucky Jun 15 '23 at 01:41
0

This should do the trick. I can't test on Windows 10 but works fine on bash in Ubuntu

google-chrome http://www.google.com

kalenpw
  • 695
  • 3
  • 10
  • 34
  • I'm getting only the message command not found even though I have the newest version for git bash –  Mar 01 '17 at 02:36
  • @Kotori, git is irrelevant here, that is a version control program. As far as the issue command not found try `open /path/to/chrome www.site.com`. or `path/to/chrome www.site.com` – kalenpw Mar 01 '17 at 02:39
0

You have few options here:

  1. Add the installation path of Google Chrome to your system path or user path. After that in your command prompt or bash shell, you can just type chrome "google.com". For me it is C:\Program Files (x86)\Google\Chrome\Application. This post explains it.
  2. Provide complete path for the chrome.exe file in your command prompt. For the above mentioned path, the command will be "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" google.com.
  3. One more option will be you can alias the complete path of exe in bash as chrome and then use it.

Please comment if you need more details.

skjoshi
  • 2,493
  • 2
  • 27
  • 38
  • is this for cmd? or git bash? –  Mar 03 '17 at 20:29
  • *UPDATE* Somehow I successfully did 3. and chrome loads up but does not put up google.com right away. Is there any way to solve this? –  Mar 03 '17 at 20:44
  • **UPDATE I guess "chrome.exe " works for windows 10 and git bash. It is working –  Mar 03 '17 at 20:51
  • `chrome.exe "example.com"` should work as far as it is in your path. It should work from command prompt and bash both being in the path. You can check path in bash by `echo $PATH` and in command prompt with `echo %PATH%` and see if Google Chrome installation path is present there. – skjoshi Mar 04 '17 at 07:25