简体   繁体   English

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

[英]how to open google chrome in git bash terminal (Windows 10)

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? 有没有办法实现这个目标?

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

start chrome www.google.com

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 ): 以下是我用来从谷歌浏览器打开URL或文件到Git bash的隐身模式 (随Git for Windows提供 ):

1. Open System Variables 1.打开系统变量

  • Hit 击中 Windows键 + R at the same time to get command prompt. + R同时获取命令提示符。
  • Then type sysdm.cpl and hit the Enter key. 然后键入sysdm.cpl并按Enter键。

运行sysdm.cpl

  • Go to Advanced 1 and select Environmental Variables 2 转到Advanced 1并选择Environmental Variables 2
  • Select Path 3 from System variables and click on Edit button 4 . System variables选择Path 3 ,然后单击Edit按钮4

Windows  - 环境变量

Keep this window opened ( while get the chrome.exe folder path ). 保持此窗口打开( 同时获取 chrome.exe 文件夹路径 )。

Windows  - 编辑环境变量

2. Get the chrome.exe folder path 2.获取chrome.exe文件夹路径

Right click ( or left if you have your mouse as left-handed ) the Chrome icon 5 ( the one you use to open it ) and select Properties 7 . 右键单击( 如果你的左手是鼠标,则左键 )Chrome图标5用于打开它的图标)并选择Properties 7 Usually this method opens more Chrome actions so locate Google Chrome option 6 and right-click on it and click on Properties 7 . 通常,此方法会打开更多Chrome操作,因此请找到Google Chrome选项6并右键单击它,然后单击Properties 7

Google Chrome快捷方式 - 属性

On the Shortcut 8 tab select the folder path from Start in: 9 ( Commonly %programfiles(x86)%\\Google\\Chrome\\Application ) Shortcut 8选项卡上,从Start in:选择文件夹路径Start in: 9通常 %programfiles(x86)%\\Google\\Chrome\\Application

Google Chrome快捷方式 - 文件夹路径

3. Adding Google Chrome folder path to Environment variables 3.将Google Chrome文件夹路径添加到Environment variables

Click on the New button 10 from previously opened Edit environment variable window and paste the Google Chrome folder path into the new added line 11 (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. 单击之前打开的Edit environment variable窗口中的“ New按钮10 ,然后将Google Chrome文件夹路径粘贴到新添加的行11中 (如果存在于路径的开头或末尾,则删除双引号)并单击所有“ OK按钮从打开的窗户。

新的环境变量

4. Add custom command to bash 4.向bash添加自定义命令

Open the Git bash terminal and edit (or create) the .bashrc file with your prefered text editor ( Visual Studio code in this example ) 打开Git bash终端并使用首选文本编辑器编辑(或创建) .bashrc文件( 本例中为Visual Studio代码

code ~/.bashrc

Git bash  - 代码.bashrc

Append the code from below. 从下面附加代码。 You can rename the function name ( chromeIt in this example) to your needs. 您可以根据需要重命名函数名称(在此示例中为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
}

If you don't want incognito mode remove the --incognito parameter. 如果您不想要隐身模式,请删除--incognito参数。

Very important note: Verify that your line ending is set as UNIX ( LF ) 12 . 非常重要的注意事项:验证您的行结尾是否设置为UNIX( LF12 If you don't know how to do it search into google . 如果您不知道该怎么做,请搜索谷歌

Visual Studio代码 - 编辑或创建.bashrc

Save the .bashrc file and reload it (using the Git bash terminal): 保存.bashrc文件并重新加载(使用Git bash终端):

source ~/.bashrc

Git bash  - 源码.bashrc

or you can use the shorter version of the command: 或者您可以使用命令的较短版本

. ~/.bashrc

Git bash  - 源.bashrc更短的版本

Close the Git bash terminal window or enter exit and re-open it. 关闭Git bash终端窗口或进入exit并重新打开它。

Try to open a URL: 尝试打开一个网址:

chromeIt google.com

Git bash  -  chromeIt open url

or a file: 或文件:

chromeIt index.html

Git bash  -  chromeIt打开文件

I hope it works for you. 我希望这个对你有用。

You can use explorer.exe that can open URL in default browser: 您可以使用可以在默认浏览器中打开URL的explorer.exe

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

Therefore there is a weird bug: URL should not contain ? 因此有一个奇怪的错误:URL不应该包含? , 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: 顺便说一下,我创建了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'

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). 在Windows上对Bash进行了测试(Ubuntu 16.04.3 LTS; GNU bash,wersja 4.3.48)。

 start http://example.com

Opens the default browser with the URL. 使用URL打开默认浏览器。

Tested on Git Bash/Windows 10. 在Git Bash / Windows 10上测试过。

This should do the trick. 这应该可以解决问题。 I can't test on Windows 10 but works fine on bash in Ubuntu 我无法在Windows 10上测试,但在Ubuntu中使用bash工作正常

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

You have few options here: 你在这里几乎没有选择:

  1. Add the installation path of Google Chrome to your system path or user path. 将Google Chrome的安装路径添加到系统路径或用户路径中。 After that in your command prompt or bash shell, you can just type chrome "google.com" . 在您的命令提示符或bash shell中,您只需键入chrome "google.com" For me it is C:\\Program Files (x86)\\Google\\Chrome\\Application . 对我来说,它是C:\\Program Files (x86)\\Google\\Chrome\\Application This post explains it. 这篇文章解释了它。
  2. Provide complete path for the chrome.exe file in your command prompt. 在命令提示符中提供chrome.exe文件的完整路径。 For the above mentioned path, the command will be "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" google.com . 对于上面提到的路径,命令将是"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. 还有一个选项是你可以将bash中的exe的完整路径别名为chrome,然后使用它。

Please comment if you need more details. 如果您需要更多详细信息,请评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM