2011-03-31 37 views
14

我試圖讓我的iTerm提示設置方式相同Paul Irish當我在Git分支中時,如何讓我的iTerm提示以不同的方式顯示?

到目前爲止,我已經中~/.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\] ' 

我不知道如何使剛剛出現的分支以不同的顏色,而不是「接通」

前述除了這一點還有其他功能,例如:

  • 顯示一個「O」的提示時,並不在一個git分支
  • 顯示一個「±」,當在一個分支
  • 顯示日期在該行的末尾

任何幫助,將理解

+0

就像一個提示:有https://github.com/djl/vcprompt做你的整個git解析。它也支持不同的vcs系統。 – 2012-01-24 08:19:24

回答

6

我剛剛寫了一篇關於如何做到這一點的文章。我已經介紹了所有的基礎知識,但必須猜測一些事情,例如保羅如何使用符號等。如果你想閱讀它,請查看http://digitalformula.net/articles/pimp-my-prompt-like-paul-irish

還有一篇關於digitalformula.net的文章,其中顯示了其他一些提示性示例 - 請參閱http://digitalformula.net/articles/a-couple-more-bash-prompt-examples

EDITED: 代碼部分如下:

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\]\[email protected]\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} ' 
+0

我是Stack Overflow的新手,所以只是想知道...是否將協議包含在響應中而不是鏈接中?我希望人們會閱讀文章,而不是在這裏獲取代碼,因爲在上面的代碼工作之前需要做一些事情。 – digitalformula 2011-05-11 03:30:28

+3

@SiegeX謹慎提及原始來源? – pablasso 2012-09-03 18:08:45

+3

@pablasso yea,me :)回顧一下,我反應過度了一下。仍然會很高興得到提及的顏色代碼來自我,但整個抄襲超過頂部,我把它回來。 – SiegeX 2012-09-04 16:52:08

7

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

PS1="\[email protected]\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\] ' 
+0

我推薦閱讀這個頁面來着色提示,因爲它也可以應用於Mac:http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt- under-linux-or-unix/ – karlphillip 2011-03-31 20:02:44

+0

如果你真的喜歡顏色:'PS1 =「\ [\ 033 [01; 32m \] \ u \ [\ 033 [36m \] @ \ h \ [\ 033 [00m \]] :\ [\ 033 [01; 34m \] \ w \ [\ 033 [00m \] \ e [1; 35m $(__ git_ps1)\ e [m \ $「' – karlphillip 2011-03-31 20:07:22

+0

我很確定您應該使用單在這裏引用,而不是雙引號。 – Flimm 2016-10-07 12:57:53

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}\[email protected]\h${WHITE}]${NORMAL}$ " 

設置標題欄是可選的。只需確保在最後使用${NORMAL}即可關閉顏色更改。

14

我使用git-aware-prompt

很多解決方案,我以前只顯示git分支,如果我只是在終端加載時的目錄。如果我在非Git倉庫中啓動iTerm,那麼當我使用git倉庫將cd放入目錄時,它就不起作用。

這個github項目解決了我的問題。

+1

謝謝,這是迄今爲止最簡單的方法,它應該更高。 – Casper 2013-06-28 14:22:34

0

功能非常豐富和廣泛的解決方案(不僅適用於iterm外殼,而且適用於Vim和其他)也是Powerline

3

如上所述,我也使用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\]\[email protected]\h\[$txtrst\] \w\$ " 

您可以更改colors to your liking

下面是一些在PS1符號的意思是:
帶有\ U - 用戶名
@ - 酷符號
\^h - 主機名
: - 酷符號分隔東西
\ W - 全對於短路徑路徑,使用\ W
\ git_branch - 當前分支的名字
\ git_dirty - 顯示*當有分支
$的改變 - 酷符號來表示,進入命令

相關問題