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

pur*_*kle 16 git macos bash 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\[\033[0:35m\]\u\[\033[0;32m\]\w\[033[0m\]$(parse_git_branch)\n\$\[\033[0m\] '
Run Code Online (Sandbox Code Playgroud)

我不知道如何使分支出现在不同的颜色而不是前面的"开"

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

  • 不在git分支时在提示符处显示"o"
  • 在分支中显示"±"
  • 在行尾显示日期

任何帮助,将不胜感激

spo*_*ong 18

我使用git-aware-prompt.

我之前有很多解决方案只显示了git分支,如果我在终端加载时只在那个目录中.如果我在非git repo中启动iTerm,那么当我cd使用git repo进入目录时它就无法工作.

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


Sie*_*geX 9

而不是使用古老的终端代码,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}$ "
Run Code Online (Sandbox Code Playgroud)

设置标题栏是可选的.请务必${NORMAL}在最后使用以关闭颜色变化.


kar*_*lip 7

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

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

哪里,

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

\e 定义颜色方案的开始

[0;35m 代表紫色

\e[m 定义方案的结束

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

PS1='\n\[\033[0;35m\]\u\[\033[0;32m\]\w\[\033[0m\]$(__git_ps1)\n\$\[\033[0m\] '
Run Code Online (Sandbox Code Playgroud)


dig*_*ula 7

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

还有一篇关于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='\[\033[0;36m\]\u@\h\[\033[01m\]:\[\033[0;35m\]\w\[\033[00m\]\[\033[1;30m\]\[\033[0;37m\]`__git_ps1 " (%s)"`\[\033[00m\]\[\033[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  :)
# \033[s = save cursor position
# \033[u = restore cursor position

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

  • @SiegeX小心提到原始来源? (3认同)
  • @pablasso是的,我:)回想一下我反应过度了一点.仍然很高兴能够提到颜色代码来自我,但是整个剽窃都超过了顶部,我把它拿回来了. (3认同)

cse*_*yam 5

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

运行此命令以快速安装:

mkdir ~/.bash
cd ~/.bash
git clone git://github.com/jimeh/git-aware-prompt.git
Run Code Online (Sandbox Code Playgroud)

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

export GITAWAREPROMPT=~/.bash/git-aware-prompt
source "${GITAWAREPROMPT}/main.sh"
Run Code Online (Sandbox Code Playgroud)

在同一文件中~/.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\$ "
Run Code Online (Sandbox Code Playgroud)

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

以下是PS1中某些符号的含义:
\ u-用户名
@-凉爽的符号
\ h-主机名
:-凉爽的符号用于分隔事物
\ w-完整路径,使用\ W表示短路径
\ git_branch-当前分支的名称
\ git_dirty-显示*当分支
$ 更改时-要表示的凉爽符号,请输入命令