如何在git bash终端中打开谷歌浏览器(Windows 10)

问题描述 投票:4回答:5

我想做一个像这样的命令:

chrome "site.com"

这将使谷歌浏览器窗口立即弹出该网站。有没有办法实现这个目标?

git bash google-chrome windows-10
5个回答
12
投票

这对我在Windows 10上使用并使用Gi​​t Bash。

start chrome www.google.com

1
投票

以下是我用来从Git bash(随incognito mode提供)向Google Chrome打开网址或文件到Git for Windows的情况:

1.打开系统变量

  • 同时按+ R以获取命令提示符。
  • 然后键入sysdm.cpl并按Enter键。

run sysdm.cpl

  • 转到Advanced1并选择Environmental Variables2
  • Path中选择System variables3并点击Edit button4。

Windows - Environment variables

保持此窗口打开(同时获取chrome.exe文件夹路径)。

Windows - Edit environment variable

2.获取chrome.exe文件夹路径

右键单击(如果您的左手是鼠标,则左侧)Chrome icon5(用于打开它的那个)并选择Properties7。通常此方法会打开更多Chrome操作,因此找到Google Chrome option6并右键单击它并单击Properties7。

Google Chrome shortcut - Properties

Shortcut8选项卡上,从Start in:9(通常%programfiles(x86)%\Google\Chrome\Application)中选择文件夹路径

Google Chrome shortcut - Folder path

3.将Google Chrome文件夹路径添加到Environment variables

单击之前打开的New窗口中的Edit environment variable button10并将Google Chrome文件夹路径粘贴到新添加的line11中(如果存在于路径的开头或末尾,则删除双引号)并单击打开的窗口中的所有OK按钮。

New environment variable

4.向bash添加自定义命令

打开Git bash终端并使用首选文本编辑器编辑(或创建).bashrc文件(本例中为Visual Studio代码)

code ~/.bashrc

Git bash - code .bashrc

从下面附加代码。您可以根据需要重命名函数名称(本例中为chromeIt)。

# 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
}

如果您不想要隐身模式,请删除--incognito参数。

非常重要的注意事项:验证您的行结尾是否设置为UNIX(LF)12。如果您不知道该怎么做,请搜索google

Visual Studio code - Edit or create .bashrc

保存.bashrc文件并重新加载(使用Git bash终端):

source ~/.bashrc

Git bash - source .bashrc

或者您可以使用命令的shorter version

. ~/.bashrc

Git bash - source .bashrc shorter version

关闭Git bash终端窗口或输入exit并重新打开它。

尝试打开一个网址:

chromeIt google.com

Git bash - chromeIt open url

或文件:

chromeIt index.html

Git bash - chromeIt open file

我希望这个对你有用。


1
投票

您可以使用可以在默认浏览器中打开URL的explorer.exe

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

因此有一个奇怪的错误:URL不应包含?,因此:

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

会失败,你需要使用:

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

解决。

顺便说一下,我创建了bash功能来打开Goog​​le页面:

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'

您可以按如下方式使用它:

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

我发现它非常有用,特别是在编码时;)

在Windows上测试Bash(Ubuntu 16.04.3 LTS; GNU bash,版本4.3.48)。


0
投票

这应该可以解决问题。我无法在Windows 10上测试,但在Ubuntu中使用bash工作正常

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


0
投票

你在这里几乎没有选择:

  1. 将Google Chrome的安装路径添加到系统路径或用户路径中。在命令提示符或bash shell之后,您只需键入chrome "google.com"即可。对我来说这是C:\Program Files (x86)\Google\Chrome\ApplicationThis post解释说。
  2. 在命令提示符中提供chrome.exe文件的完整路径。对于上面提到的路径,命令将是"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" google.com
  3. 还有一个选项是你可以将bash中的exe的完整路径别名为chrome,然后使用它。

如果您需要更多详细信息,请评论。

© www.soinside.com 2019 - 2024. All rights reserved.