2013-06-27 80 views
38

我一直在觀看一些Team Treehouse視頻,他們在使用Git時有一個非常漂亮的終端。如何在終端中顯示當前分支和文件夾路徑?

例如他們有(類似的東西):

[email protected]: [/Work/test - feature-branch-name] $ git add . 
[email protected]: [/Work/test - feature-branch-name] $ git commit -m "Some feature." 
[email protected]: [/Work/test - feature-branch-name] $ git checkout master 
[email protected]: [/Work/test - master] $ git status 

我的終端如何能告訴我的,我什麼分支一些有用的信息,以顏色來區分我想要的數據位?有沒有找到某種事實上的插件?

我使用的是Mac OSX 10.8

回答

45

這不是關於插件。這是關於shell中的提示技巧。

對於在bash陰涼設置,看看這傢伙的dotfiles項目:

https://github.com/mathiasbynens/dotfiles

爲了得到一個奇特的提示,包括在.bash_prompt~/.bash_profile~/.bashrc

得到確切的相同的提示在你的問題,改變export PS1線在.bash_prompt末是這樣的:

export PS1="\[${BOLD}${MAGENTA}\]\u\[$WHITE\]@\[$ORANGE\]\h\[$WHITE\]: [\[$GREEN\]\w\[$WHITE\]\$([[ -n \$(git branch 2> /dev/null) ]] && echo \" - \")\[$PURPLE\]\$(parse_git_branch)\[$WHITE\]] \$ \[$RESET\]" 

我大約一個月前結束了使用所有.bash*文件從這個倉庫,這對我來說真的很有用。

對於Git,.gitconfig還有額外的好東西。

既然你是一個mac用戶,在.osx還有更多的好東西。

+0

這之後我得到:'bash下parse_git_branch:命令不found' –

2

安裝在系統上的git包含bash文件以幫助您創建信息提示。要創建顏色,您需要將終端轉義序列插入到提示中。最後的組成部分是通過使用內置變量PROMPT_COMMAND執行每個命令後更新提示。

編輯您的〜/ .bashrc以包含以下內容,並且您應該在您的問題中獲得提示,以某些顏色差異爲模。

# 
# Git provides a bash file to create an informative prompt. This is its standard 
# location on Linux. On Mac, you should be able to find it under your Git 
# installation. If you are unable to find the file, I have a copy of it on my GitHub. 
# 
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/40-git-prompt.sh 
# 
source /usr/share/git/completion/git-prompt.sh 

# 
# Next, we need to define some terminal escape sequences for colors. For a fuller 
# list of colors, and an example how to use them, see my bash color file on my GitHub 
# and my coniguration for colored man pages. 
# 
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/10-colors.sh 
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/40-less.sh 
# 
color_start='\e[' 
color_end='m' 
color_reset='\e[0m' 
color_bg_blue='44' 

# 
# To get a fancy git prompt, it's not sufficient to set PS1. Instead, we set PROMPT_COMMAND, 
# a built in Bash variable that gets evaluated before each render of the prompt. 
# 
export PROMPT_COMMAND="PS1=\"\${color_start}\${color_bg_blue}\${color_end}\[email protected]\h [\w\$(__git_ps1 \" - %s\")]\${color_reset}\n\$ \"" 

# 
# If you find that the working directory that appears in the prompt is ofter too long, 
# then trim it. 
# 
export PROMPT_DIRTRIM=3 
35
在你喜歡的編輯器

簡單的方式

打開~/.bash_profile,並添加以下內容底部。

提示中的Git分支。

parse_git_branch() { 
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' 
} 

export PS1="\[email protected]\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ " 

ADD GIT BRANCH NAME TO TERMINAL PROMPT (MAC)

+1

我喜歡這個用\ n在最後的$符號之前把我的提示放在下一行。 – Gandalf458

+0

花了5分鐘完全定製!這裏是顏色:https://misc.flogisoft.com/bash/tip_colors_and_formatting – rndrfero

25

爲了擴大現有偉大的答案,一個非常簡單的方式來獲得一個非常好看的終端要使用開源點文件項目。

https://github.com/mathiasbynens/dotfiles


enter image description here


安裝上OSX和Linux死的簡單。在終端中運行以下命令。

git clone https://github.com/mathiasbynens/dotfiles.git && cd dotfiles && source bootstrap.sh 

這將:

  1. 的Git克隆回購。
  2. cd進入文件夾。
  3. 運行安裝bash腳本。
+3

任何想法如何扭轉這? – ChickenWing24

+0

找到一種方法來扭轉它@ ChickenWing24? – ollaollu

+0

@ollaollu我做了一些反向處理。雖然時間很長,所以無法回憶起100%的步驟。但我所做的基本上是刪除與該回購相關的所有文件/文件夾。 – ChickenWing24

4

我的提示包括:最後一個命令的

  • 退出狀態(如果不是0)
  • 截然不同的變化時,根
  • rsync風格[email protected]:pathname的複製粘貼善良
  • 的Git分支,索引,修改,未跟蹤和上游信息
  • 漂亮的顏色

例子: Screenshot of my prompt in action 要做到這一點,添加以下到您的~/.bashrc

# 
# Set the prompt # 
# 

# Select git info displayed, see /usr/lib/git-core/git-sh-prompt for more 
export GIT_PS1_SHOWDIRTYSTATE=1   # '*'=unstaged, '+'=staged 
export GIT_PS1_SHOWSTASHSTATE=1   # '$'=stashed 
export GIT_PS1_SHOWUNTRACKEDFILES=1  # '%'=untracked 
export GIT_PS1_SHOWUPSTREAM="verbose"  # 'u='=no difference, 'u+1'=ahead by 1 commit 
export GIT_PS1_STATESEPARATOR=''   # No space between branch and index status 
export GIT_PS1_DESCRIBE_STYLE="describe" # detached HEAD style: 
# contains  relative to newer annotated tag (v1.6.3.2~35) 
# branch  relative to newer tag or branch (master~4) 
# describe  relative to older annotated tag (v1.6.3.1-13-gdd42c2f) 
# default  exactly eatching tag 

# Check if we support colours 
__colour_enabled() { 
    local -i colors=$(tput colors 2>/dev/null) 
    [[ $? -eq 0 ]] && [[ $colors -gt 2 ]] 
} 
unset __colourise_prompt && __colour_enabled && __colourise_prompt=1 

__set_bash_prompt() 
{ 
    local exit="$?" # Save the exit status of the last command 

    # PS1 is made from $PreGitPS1 + <git-status> + $PostGitPS1 
    local PreGitPS1="${debian_chroot:+($debian_chroot)}" 
    local PostGitPS1="" 

    if [[ $__colourise_prompt ]]; then 
     export GIT_PS1_SHOWCOLORHINTS=1 

     # Wrap the colour codes between \[ and \], so that 
     # bash counts the correct number of characters for line wrapping: 
     local Red='\[\e[0;31m\]'; local BRed='\[\e[1;31m\]' 
     local Gre='\[\e[0;32m\]'; local BGre='\[\e[1;32m\]' 
     local Yel='\[\e[0;33m\]'; local BYel='\[\e[1;33m\]' 
     local Blu='\[\e[0;34m\]'; local BBlu='\[\e[1;34m\]' 
     local Mag='\[\e[0;35m\]'; local BMag='\[\e[1;35m\]' 
     local Cya='\[\e[0;36m\]'; local BCya='\[\e[1;36m\]' 
     local Whi='\[\e[0;37m\]'; local BWhi='\[\e[1;37m\]' 
     local None='\[\e[0m\]' # Return to default colour 

     # No username and bright colour if root 
     if [[ ${EUID} == 0 ]]; then 
      PreGitPS1+="$BRed\h " 
     else 
      PreGitPS1+="$Red\[email protected]\h$None:" 
     fi 

     PreGitPS1+="$Blu\w$None" 
    else # No colour 
     # Sets prompt like: [email protected]:~/prj/sample_app 
     unset GIT_PS1_SHOWCOLORHINTS 
     PreGitPS1="${debian_chroot:+($debian_chroot)}\[email protected]\h:\w" 
    fi 

    # Now build the part after git's status 

    # Highlight non-standard exit codes 
    if [[ $exit != 0 ]]; then 
     PostGitPS1="$Red[$exit]" 
    fi 

    # Change colour of prompt if root 
    if [[ ${EUID} == 0 ]]; then 
     PostGitPS1+="$BRed"'\$ '"$None" 
    else 
     PostGitPS1+="$Mag"'\$ '"$None" 
    fi 

    # Set PS1 from $PreGitPS1 + <git-status> + $PostGitPS1 
    __git_ps1 "$PreGitPS1" "$PostGitPS1" '(%s)' 

    # echo '$PS1='"$PS1" # debug  
    # defaut Linux Mint 17.2 user prompt: 
    # PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[01;34m\] \w\[\033[00m\] $(__git_ps1 "(%s)") \$ ' 
} 

# This tells bash to reinterpret PS1 after every command, which we 
# need because __git_ps1 will return different text and colors 
PROMPT_COMMAND=__set_bash_prompt 
+0

這是令人驚訝的乾淨的代碼爲信息包裝提示。我花了好幾個小時的時間來處理我的提示並爬出陷阱,基本上達到了你在這裏的目標。如果在提示符上使用自定義顏色的git狀態,我強烈建議您從此代碼開始。 – wisbucky

相關問題