簡體   English   中英

當我在Git分支時,如何讓我的iTerm提示以不同的方式顯示?

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

我試圖讓我的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\] '

我不知道如何使分支出現在不同的顏色而不是前面的“開”

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

  • 不在git分支時在提示符處顯示“o”
  • 在分支中顯示“±”
  • 在行尾顯示日期

任何幫助,將不勝感激

我使用git-aware-prompt

我之前有很多解決方案只顯示了git分支,如果我在終端加載時只在那個目錄中。 如果我在非混帳回購協議開始的iTerm,那就不是在工作的時候cd到了Git的回購目錄。

這個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 on\e[0;35m$(__git_ps1)\e[m\$ "

哪里,

$(__git_ps1)用於打印分支名稱

\\e定義顏色方案的開始

[0;35m代表紫色

\\e[m定義了方案的結束

另外 ,我修復了你當前的提示:

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

我剛剛寫了一篇關於如何做到這一切的帖子。 我已經涵蓋了所有的基礎知識,但不得不猜測一些事情,例如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} '

如上所述,我也使用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等)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM