bash 当我在 Git 分支中时,如何让我的 iTerm 提示以不同的方式显示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5505302/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:21:06  来源:igfitidea点击:

How do I get my iTerm prompt to display differently when I'm in a Git branch?

gitmacosbashiterm

提问by purinkle

I'm trying to get my iTerm prompt set up the same way as Paul Irish

我正在尝试以与Paul Irish相同的方式设置 iTerm 提示

So far I have the following in ~/.profile:

到目前为止,我有以下内容~/.profile

# Add git branch name to prompt
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/*\(.*\)/ on /'
}

PS1='\n\[3[0:35m\]\u\[3[0;32m\]\w\[033[0m\]$(parse_git_branch)\n$\[3[0m\] '

I don't know how to make just the branch appear in a different colour and not the preceding "on"

我不知道如何让分支出现不同的颜色而不是前面的“on”

As well as this there are other features such as:

除此之外,还有其他功能,例如:

  • Displaying an "o" at the prompt when not in a git branch
  • Displaying a "±" when in a branch
  • Displaying the date at the end of the line
  • 不在 git 分支中时在提示符处显示“o”
  • 在分支中显示“±”
  • 在行尾显示日期

Any help would be appreciated

任何帮助,将不胜感激

采纳答案by digitalformula

I've just written a post about how to do all this. I've covered all the basics but had to guess a couple of things, e.g. how Paul uses the symbols etc. If you want to read it, check out http://digitalformula.net/articles/pimp-my-prompt-like-paul-irish.

我刚刚写了一篇关于如何做到这一切的帖子。我已经涵盖了所有基础知识,但不得不猜测一些事情,例如保罗如何使用符号等。如果您想阅读它,请查看http://digitalformula.net/articles/pimp-my-prompt-like -保罗爱尔兰

There's also an article on digitalformula.net that shows a couple of other prompt examples - see http://digitalformula.net/articles/a-couple-more-bash-prompt-examples.

在 digitalformula.net 上还有一篇文章,其中显示了一些其他提示示例 - 请参阅http://digitalformula.net/articles/a-couple-more-bash-prompt-examples

EDITED: The code part is as follows:

编辑:代码部分如下:

PATH=$PATH:~/Data/Scripts:~/Data/Utils/rar:~/_Applications:~/_Applications/lynx

# alias to quickly show if any Handbrake processes are running
alias hb='sudo ps -aef | grep HandBrakeCLI'

# alias for quick DNS cache flushing
alias fc='sudo dscacheutil -flushcache'

# enable the git bash completion commands
source ~/.git-completion

# enable git unstaged indicators - set to a non-empty value
GIT_PS1_SHOWDIRTYSTATE="."

# enable showing of untracked files - set to a non-empty value
GIT_PS1_SHOWUNTRACKEDFILES="."

# enable stash checking - set to a non-empty value
GIT_PS1_SHOWSTASHSTATE="."

# enable showing of HEAD vs its upstream
GIT_PS1_SHOWUPSTREAM="auto"

BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
LIME_YELLOW=$(tput setaf 190)
POWDER_BLUE=$(tput setaf 153)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BRIGHT=$(tput bold)
NORMAL=$(tput sgr0)
BLINK=$(tput blink)
REVERSE=$(tput smso)
UNDERLINE=$(tput smul)

# set the prompt to show current working directory and git branch name, if it exists

# this prompt is a green username, black @ symbol, cyan host, magenta current working directory and white git branch (only shows if you're in a git branch)
# unstaged and untracked symbols are shown, too (see above)
# this prompt uses the short colour codes defined above
# PS1='${GREEN}\u${BLACK}@${CYAN}\h:${MAGENTA}\w${WHITE}`__git_ps1 " (%s)"`$ '

# this is a cyan username, @ symbol and host, magenta current working directory and white git branch
# it uses the shorter , but visibly more complex, codes for text colours (shorter because the colour code definitions aren't needed)
# PS1='\[3[0;36m\]\u@\h\[3[01m\]:\[3[0;35m\]\w\[3[00m\]\[3[1;30m\]\[3[0;37m\]`__git_ps1 " (%s)"`\[3[00m\]\[3[0;37m\]$ '

# return the prompt prefix for the second line
function set_prefix {
    BRANCH=`__git_ps1`
    if [[ -z $BRANCH ]]; then
        echo "${NORMAL}o"
    else
        echo "${UNDERLINE}+"
    fi
}

# and here's one similar to Paul Irish's famous prompt ... not sure if this is the way he does it, but it works  :)
# 3[s = save cursor position
# 3[u = restore cursor position

PS1='${MAGENTA}\u${WHITE} in ${GREEN}\w${WHITE}${MAGENTA}`__git_ps1 " on %s"`${WHITE}\r\n`set_prefix`${NORMAL}${CYAN}3[s3[60C (`date "+%a, %b %d"`)3[u${WHITE} '

回答by spong

I use git-aware-prompt.

我使用git-aware-prompt

A lot of solutions I had before only displayed the git branch if I were only in that directory when the terminal loaded. If I started iTerm in a non-git repo, then it wouldn't work when I cdinto directory with a git repo.

如果我在终端加载时仅在该目录中,我以前的许多解决方案只显示 git 分支。如果我在非 git 存储库中启动 iTerm,那么当我cd使用 git 存储库进入目录时它将无法工作。

This github project solved that for me.

这个 github 项目为我解决了这个问题。

回答by SiegeX

Rather than using archaic terminal codes, use tputinstead which makes the code much easier to read and a lot harder to mess up:

与其使用过时的终端代码,不如使用tput它使代码更易于阅读且更难弄乱:

BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
LIME_YELLOW=$(tput setaf 190)
POWDER_BLUE=$(tput setaf 153)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BRIGHT=$(tput bold)
NORMAL=$(tput sgr0)
BLINK=$(tput blink)
REVERSE=$(tput smso)
UNDERLINE=$(tput smul)

# Set Titlebar and Prompt
TITLEBAR='\e]0;\h: ${PWD/$HOME/~}\a'
PS1="${TITLEBAR}${WHITE}[${POWDER_BLUE}\u@\h${WHITE}]${NORMAL}$ "

Setting the titlebar is optional. Just be sure to use ${NORMAL}at the end to turn off the color change.

设置标题栏是可选的。只需确保${NORMAL}在最后使用以关闭颜色更改。

回答by karlphillip

Add this to your ~/.bashrcor ~/.profile

将此添加到您的~/.bashrc~/.profile

PS1="\u@\h:\w on\e[0;35m$(__git_ps1)\e[m$ "

Where,

在哪里,

$(__git_ps1)is used for printing the branch name

$(__git_ps1)用于打印分支名称

\edefines the start of the color scheme

\e定义配色方案的开始

[0;35mrepresent the purple color

[0;35m代表紫色

\e[mdefines the end of the scheme

\e[m定义方案的结束

Also, I fixed your current prompt:

另外,我修复了您当前的提示:

PS1='\n\[3[0;35m\]\u\[3[0;32m\]\w\[3[0m\]$(__git_ps1)\n$\[3[0m\] '

回答by csebryam

As mentioned above, I also use git-aware-prompt.

如上所述,我也使用git-aware-prompt

Run this to quickly install:

运行它以快速安装:

mkdir ~/.bash
cd ~/.bash
git clone git://github.com/jimeh/git-aware-prompt.git

Add this to the top of your ~/.bash_profile:

将此添加到您的顶部~/.bash_profile

export GITAWAREPROMPT=~/.bash/git-aware-prompt
source "${GITAWAREPROMPT}/main.sh"

In the same file ~/.bash_profilehere is the prompt I use:

在同一个文件中,~/.bash_profile这里是我使用的提示:

export PS1="\n\[$txtpur\]\u\[$bldwht\]@\h\[$bldgrn\]:\[$bldblu\] \w \[$txtcyn\]$git_branch\[$txtred\]$git_dirty\[$txtrst\]$ \[$txtwht\] "

export SUDO_PS1="\[$bakred\]\u@\h\[$txtrst\] \w$ "

you can change the colors to your liking

您可以根据自己的喜好更改颜色

Here is what some of the symbols in PS1 mean:
\u - username
@ - cool symbol
\h - hostname
: - cool symbol to separate things
\w - full path, use \W for short path
\git_branch - name of current branch
\git_dirty - show * when there is a change in branch
$ - cool symbol to signify, enter command

以下是 PS1 中一些符号的含义:
\u - 用户名
@ - 酷符号
\h - 主机名
:- 分隔事物的酷符号
\w - 完整路径,使用 \W 作为短路径
\git_branch - 当前分支的名称
\ git_dirty - 显示 * 当分支发生变化时
$ - 很酷的符号来表示,输入命令

回答by Geert

A very feature rich and broad solution (not only for iterm shell but also for Vim and others) is Powerline.

一个功能非常丰富且广泛的解决方案(不仅适用于 iterm shell,也适用于 Vim 和其他)是Powerline