PS1 行与 Git 当前分支和颜色
这是我当前的 PS1:
export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
如何以不同的颜色显示当前分支?
Here is my current PS1:
export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
How can I display the current branch in a different color?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
这是一部分(没有 Ruby):
看起来像这样(使用我自己的终端调色板):
另外,请参阅此和这篇文章。
Here is, part by part (and no Ruby):
Looks like this (with my own terminal palette):
Also, see this and this article.
您可以使用以下内容将所需颜色的部分包裹起来:
\e[0;32m
- 设置颜色(在本例中为绿色)\e[m
-将颜色设置回默认值例如,这会将提示设置为当前路径的最后一个标记(绿色),后跟默认颜色的
$
:也可以使用其他颜色。请参阅这篇文章的彩色部分以获得完整的列表的替代方案。
You can wrap the part that you want in colour with the following:
\e[0;32m
- sets colour (in this case, to green)\e[m
- sets colour back to the defaultFor example, this sets the prompt to the last token of the current path, in green, followed by
$
in the default colour:Other colours are available too. Have a look at this article under colorization for a comprehensive list of alternatives.
这是我的 PS1 行:
Here is my PS1 line:
来源
Source
这是我的 PS1 解决方案。
它在带有小说主题的 Mac 上看起来很棒。
抱歉,但我的缩进有点被破坏了。破解它直到你喜欢为止。
如果您在分行结账,您会得到分行名称。
如果您处于刚刚初始化的 Git 项目中,您只需得到“@”。
如果你是无头的,你会得到一个与某个分支或标签相关的好听的人名,名字前面有一个“@”。
如果你是无头的并且不是某个分支或标签的祖先,你只会得到短 SHA1。
另外,红色“-”表示干净的工作目录和索引,红色“+”表示相反。
This is my PS1 solution.
It looks great on a Mac with the Novel theme.
Sorry, but my indentation got munged a bit. Hack it till you like it.
If you are checked out at a branch, you get the branch name.
If you are in a just init'd Git project, you just get '@'.
If you are headless, you get a nice human name relative to some branch or tag, with an '@' preceding the name.
If you are headless and not an ancestor of some branch or tag you just get the short SHA1.
In addition, a red '-' signifies a clean work directory and index, and a red '+' signifies the opposite.
我的超级强大的多行 Linux 提示符!
将其放在 .bashrc 中或更好:将其保存在 /etc/bash-prompt 中并从 .bashrc 中获取它。
使用 tput 应该是处理颜色的正确方法。
My uber-powerful multi-line Linux prompt!
Put it either in your .bashrc or better: save it in /etc/bash-prompt and source it from your .bashrc.
Using tput is supposed to be the right way to do colors.
~/.bashrc
:~/.bashrc
:更多详细信息:https://medium.com/@ thucnc/如何显示当前 git-branch-with-colors-in-bash-prompt-380d05a24745
~/.bashrc
:~/.bashrc
:More detail: https://medium.com/@thucnc/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745
@cmcginty 提示符的修改版本,添加了
git
解析函数并使用略有不同的间距:这也在提示符中使用
\$
而不是$
>,这意味着当您是 root 时,您将得到#
。Modified version of @cmcginty's prompt that adds in the
git
parsing function and uses slightly different spacing:This also uses
\$
in the prompt instead of$
, which means you will get#
when you are root.对于我的带有 Homebrew 主题的 Mac,这效果非常好。完全调试且速度非常快,并且完全独立。
额外奖励:足够智能,当您实际上位于 git 存储库中时,仅将 git 分支显示为提示的一部分! :)
它看起来像这样:
Mac + Homebrew + Color Git Prompt
如果您想要完整路径(或删除 . ../),然后只需将 -W 更改为 -w (并删除 .../)。
For my Mac with the Homebrew theme, this works really well. Fully debugged and very fast, and completely self-contained.
BONUS: Smart enough to ONLY show a git branch as part of the prompt when you're actually in a git repo! :)
Here's what it looks like:
Mac + Homebrew + Color Git Prompt
If you want to have the full path (or remove the .../), then just change the -W to -w (and remove the .../).
看看liquidprompt:
https://github.com/nojhan/liquidprompt
可能有点太重了 来关闭功能,
您的要求,但您可以通过设置
LP_ENABLE_...=0
请参阅上页的文档。
Take a look at liquidprompt:
https://github.com/nojhan/liquidprompt
Maybe a bit too heavy for your requirements, but you can switch features off by setting
LP_ENABLE_...=0
See the documentation on above page.
只需使用适当的参数调用
tput
即可。请参阅tput(1)
和terminfo(5)
手册页。Just invoke
tput
with the appropriate parameters. See thetput(1)
andterminfo(5)
man pages.这是我的
Here's mine
这个 PS1 会将你当前的 git 分支染成黄色:
要以更具交互性的方式查看如何构建 PS1 字符串(显然其他方法也是可能的),这里有一个非常方便的 PS1 字符串生成器的链接。 bashrc。我将它用于上面的字符串:
http://bashrcgenerator.com/
它以简单的方式解决了您的问题更普遍的是构建自定义和彩色 shell 提示符的问题,包括当前的 git 分支
This PS1 will color your current git branch in yellow:
To see in a more interactive way the how the building of a PS1 string can be done (other ways are possible obviously), here the link to a very handy PS1 string generator for your .bashrc. I used it for the string above:
http://bashrcgenerator.com/
It solves in a simple way your question and more generally the issue of building of a customized and colorized shell prompt, including the current git branch
对于更复杂的 Git 状态,您可以使用一些更大的脚本。
For more complicated status of Git you can use some larger script.
我是这样做的:
这段代码应该转到
~/.bashrc
。结果如下所示:代码位于此处
Here is how I did this:
This code is supposed to go to
~/.bashrc
. The result looks like this:Code is available here
这是一个 Windows/Cygwin/Bash 解决方案。
将以下内容添加到您的
~/.bashrc
文件中。xxx 是本地 Git 存储库的位置。
Here is a Windows/Cygwin/Bash solution.
Add the following to your
~/.bashrc
file.xxx is the location of your local Git repository.