在 Windows 上的 Git Bash 中设置提示颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10481685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting colors for prompt in Git Bash on Windows
提问by marc_s
I've successfully played around with some of the color settings in the Git Bash on Windows - I'm able to set a few things, like the colors of the local, the current and remote branches in my .gitconfigfile:
我已经成功地在 Windows 上的 Git Bash 中使用了一些颜色设置 - 我能够设置一些东西,比如我的.gitconfig文件中本地、当前和远程分支的颜色:
[color "branch"]
current = cyan bold
local = cyan
remote = red
But what I haven't managed to change are the colors of the prompt - the username@machineat the beginning of the line (in the yellow rectangle in my screenshot), and the project and branch I'm currently on (purple rectangle).
但是我没有设法改变提示的颜色 -username@machine行首(在我的屏幕截图中的黄色矩形中),以及我当前所在的项目和分支(紫色矩形)。


Is there a way to influence those, too? Which .gitconfigsettings do I need to set to change those colors?
有没有办法影响这些?.gitconfig我需要设置哪些设置来更改这些颜色?
采纳答案by Paused until further notice.
In your .bashrcyou can set your prompt using the PS1variable (which is likely set to a global value in /etc/profileor another file in /etcwhich may be distribution dependent).
在您的文件中,.bashrc您可以使用PS1变量设置提示(该变量可能设置为全局值/etc/profile或其他/etc可能依赖于分布的文件)。
Here's an example:
下面是一个例子:
PS1='\[3[1;36m\]\u@\h:\[3[0m\]\[3[1;34m\]\w\[3[0m\] \[3[1;32m\]$(__git_ps1)\[3[0m\]$ '
In order for the command substitution to work, you need shopt -s promptvarswhich is the default.
为了使命令替换起作用,您需要shopt -s promptvars哪个是默认值。
This will output the user and hostname in cyan, the current directory in blue and the git branch in green on terminals that work with TERM=xterm-color.
这将输出青色的用户和主机名,蓝色的当前目录和绿色的 git 分支在使用TERM=xterm-color.
See man 5 terminfoand man tputfor more information about terminal controls.
有关终端控件的更多信息,请参阅man 5 terminfo和man tput。

