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

我试图让我的iTerm提示设置方式与Paul Irish相同 到目前为止,我在
~/.profile
中有以下内容:
# Add git branch name to prompt
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/*(.*)/ on 1/'
}

PS1='n[33[0:35m]u[33[0;32m]w[033[0m]$(parse_git_branch)n$[33[0m] '
我不知道如何使分支出现在不同的颜色而不是前面的“开” 除此之外还有其他功能,例如: 不在git分支时在提示符处显示“o” 在分支中显示“±” 在行尾显示日期 任何帮助,将不胜感激     
已邀请:
我刚刚写了一篇关于如何做到这一切的帖子。我已经涵盖了所有的基础知识,但不得不猜测一些事情,例如: Paul如何使用符号等。如果您想阅读它,请查看http://digitalformula.net/articles/pimp-my-prompt-like-paul-irish。 还有一篇关于digitalformula.net的文章,其中展示了其他一些提示示例 - 请参阅http://digitalformula.net/articles/a-couple-more-bash-prompt-examples。 编辑: 代码部分如下:
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='[33[0;36m]u@h[33[01m]:[33[0;35m]w[33[00m][33[1;30m][33[0;37m]`__git_ps1 " (%s)"`[33[00m][33[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  :)
# 33[s = save cursor position
# 33[u = restore cursor position

PS1='${MAGENTA}u${WHITE} in ${GREEN}w${WHITE}${MAGENTA}`__git_ps1 " on %s"`${WHITE}rn`set_prefix`${NORMAL}${CYAN}33[s33[60C (`date "+%a, %b %d"`)33[u${WHITE} '
    
我使用git-aware-prompt。 我之前有很多解决方案只显示了git分支,如果我在终端加载时只在那个目录中。如果我在非git repo中启动iTerm,那么当我使用git repo进入目录时它就无法工作。 这个github项目为我解决了这个问题。     
而不是使用古老的终端代码,而不是使用
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}$ "
设置标题栏是可选的。请务必在末尾使用
${NORMAL}
来关闭颜色变化。     
将此添加到
~/.bashrc
~/.profile
PS1="u@h:w one[0;35m$(__git_ps1)e[m$ "
哪里,
$(__git_ps1)
用于打印分支名称
e
定义了配色方案的开始
[0;35m
代表紫色
e[m
定义了方案的结束 另外,我修复了你当前的提示:
PS1='n[33[0;35m]u[33[0;32m]w[33[0m]$(__git_ps1)n$[33[0m] '
    
如上所述,我也使用git-aware-prompt。 运行此命令以快速安装:
mkdir ~/.bash
cd ~/.bash
git clone git://github.com/jimeh/git-aware-prompt.git
将其添加到
~/.bash_profile
的顶部:
export GITAWAREPROMPT=~/.bash/git-aware-prompt
source "${GITAWAREPROMPT}/main.sh"
在同一个文件中
~/.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$ "
你可以根据自己的喜好改变颜色   这是PS1中的一些符号的含义:    u - 用户名   @ - 酷符号    h - 主机名   : - 炫酷的符号来分隔事物    w - 完整路径,使用 W表示短路径    git_branch - 当前分支的名称    git_dirty - 当分支发生变化时显示*   $ - 用于表示的酷符号,输入命令     
Powerline是一个功能丰富且功能广泛的解决方案(不仅适用于iterm shell,也适用于Vim等)。     

要回复问题请先登录注册