在Linux上显示当前分支和着色(如Windows上的Git Bash)

时间:2017-02-13 16:36:32

标签: linux git git-bash

在Windows上的Git Bash上,我得到了很好的着色,显示了当前的分支,如下所示:

enter image description here

如何在Linux上获得相同的颜色和提示?在Linux上我使用常规终端,它不显示当前分支。

3 个答案:

答案 0 :(得分:2)

如果您想保留现有 linux 终端的相同颜色并显示当前的 git 分支,您可以将以下内容添加到您的默认 PS1。

PS1 基本上告诉你的 bash 提示显示什么。 参考:How to Change / Set up bash custom prompt (PS1) in Linux

我使用的是 Ubuntu 20.04,默认 PS1 位于 ~/.bashrc 文件中的 if [ "$color_prompt" = yes ]; then 下。

步骤:

  1. 打开 ~/.bashrc 文件,找到 if [ "$color_prompt" = yes ]; then
  2. 在 if 语句上方定义以下 bash 函数(感谢 @Nogoseke)(您可以在文件中 PS1 值上方的任何位置定义它)。
#show git branch
show_git_branch() {
   git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
  1. 将 PS1 值更新为以下内容:
if [ "$color_prompt" = yes ]; then
    PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[\033[31m\]\$(show_git_branch)\[\033[00m\]$ "
else
    PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w \$(show_git_branch)\$ "
fi

整个事情应该是这样的:

#show git branch
show_git_branch() {
   git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

if [ "$color_prompt" = yes ]; then
    PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[\033[31m\]\$(show_git_branch)\[\033[00m\]$ "
else
    PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w \$(show_git_branch)\$ "
fi
  1. 保存更改并重启您的终端。

它应该是这样的: Terminal with git branch

答案 1 :(得分:0)

如果您使用的是bash,

我在〜/ .bashrc中使用以下内容:

show_git_branch() {
   git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
 }
 export PS1="\[\033[0;37m\]\u@\h\[\033[0;37m\] \w \[\033[31m\]\$(show_git_branch)\[\033[00m\]$\[\033[00m\] "

它的样子:

enter image description here

您只需将代码添加到.bashrc文件中并运行source ~/.bashrc

答案 2 :(得分:-1)

我正在使用oh-my-zshmacOS中实现相同功能,因为Linux也可以使用bash,但您可以尝试尝试从zsh切换到sap.ui.xmlfragment("firstapp.flightdata.view." + wa[j].getAttribute("value"), this).placeAt(wa[j].getAttribute("id")); {1}}。