21

Paul Irishと同じ方法で 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\] '

前の「オン」ではなく、枝だけを別の色で表示する方法がわかりません

これに加えて、次のような他の機能があります。

  • gitブランチにないときにプロンプ​​トに「o」を表示する
  • 分岐中は「±」を表示
  • 行末に日付を表​​示する

どんな助けでもいただければ幸いです

4

6 に答える 6

23

私はgit-aware-promptを使用しています。

以前の多くのソリューションでは、ターミナルがロードされたときにそのディレクトリにいる場合にのみ、git ブランチを表示していました。git 以外のリポジトリで iTerm を起動した場合cd、git リポジトリのあるディレクトリに入ると機能しません。

このgithubプロジェクトはそれを解決しました。

于 2013-04-01T19:34:56.397 に答える
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}$ "

タイトルバーの設定はオプションです。必ず${NORMAL}最後に使用して色の変更をオフにしてください。

于 2011-03-31T19:39:40.157 に答える
8

これをすべて行う方法についての投稿を書きました。基本的なことはすべて説明しましたが、ポールが記号をどのように使用しているかなど、いくつか推測する必要がありました。読みたい場合は、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} '
于 2011-05-08T12:37:47.887 に答える
7

~/.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\] '
于 2011-03-31T19:39:24.783 に答える
0

非常に機能が豊富で幅広いソリューション (iterm シェルだけでなく、Vim など) はPowerlineです。

于 2015-01-28T08:02:41.617 に答える