2013-06-27 5 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

これはプラグインに関するものではありません。それは、シェルの迅速なトリックについてです。 bashでクールなセットアップのために

、この男のdotfilesプロジェクトをチェックアウト:ファンシープロンプトを取得するには

https://github.com/mathiasbynens/dotfiles

は、あなたの~/.bash_profileまたは~/.bashrc.bash_promptが含まれます。 、私は約1カ月前、このリポジトリからすべての.bash*ファイルを使用して終了

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_promptの終わりにexport PS1行を変更し、あなたの質問のようにまったく同じプロンプトを取得するにはそれは私のために本当に便利でした。

Gitの場合、.gitconfigには別の特典があります。

あなたがMacユーザーであるため、.osxにさらに多くの特典があります。

+0

この後、私は取得しています: 'bashの:parse_git_branch:コマンドは、私は\ nにこれを言ってい –

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

をfound'ありません最後の$記号の前に、私のプロンプトを次の行に置きます。 – 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

関連する問題