Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Git info in right prompt



Hi John,

>>>>> "John" == John Magolske <listmail@xxxxxxx> writes:

    John> Hi,
    John> I'd like to display the current git branch in the right prompt,
    John> similar to what's shown here:

    John>   http://news.ycombinator.com/item?id=767485

    John> But I'm having a hard time making out what's being suggested for
    John> the zshrc (partially due to poor formatting), maybe this? :

    John>   # precmd is called just before the prompt is printed
    John>   function
    John>   precmd() { title "zsh" "$USER@%m" "%55<...<%~"
    John>   psvar=()
    John>   vcs_info
    John>   [[ -n $vcs_info_msg_0_ ]] && psvar[1]="$vcs_info_msg_0_"
    John>   }

    John>   vcs_info export RPS1="${YELLOW}%1v${NORM}"

    John> But I don't think that's quite right...and I can't seem to make any
    John> "display git branch" thing work with my current prompt:

    John>   PS1=$'%{\e[01;32m%} %~ %{\e[01;33m%}%# %{\e[0m%}'

Below is my setup for my prompts; it's a bit *more* than what you ask for, but
it is hopefully helpful.

It has the following properties:
- put the branch in the right prompt if we're in some directory with 'src' in
  otherwise show nothing (it works for cvs/svn/bzr/git here at least)
- show the return code of the last command in the prompt, but only if it was
  nonzero
- use different colors for different machines/users (I'm using the same .zshrc
  on different machines/users)

Overall, it's a combination of snippets I found in different places; I'm sure
it can be improved, but it works pretty well for me.

Best wishes,
Dirk.


    
################################################################################
# prompt
autoload colors zsh/terminfo
colors
setopt prompt_subst

# # Define common and useful things to put in a prompt
hostname="`hostname`"

#
# use host / user specific colors
# 
case `hostname` in
    foo)
	hostnamecolor="cyan";;
    bar)
	hostnamecolor="magenta";;
    cuux)
        hostnamecolor="green";;
    *)
	hostnamecolor="default";;
esac


case $USER in
    root)
	usernamecolor="red";;
    jim)
	usernamecolor="blue";;
    bob)
        usernamecolor="yellow";;
    *)
	usernamecolor="default";;
esac

# # Declare an associative array, "_pr_ompt _c_omponents"
typeset -A prc

prc[abbrevpath]='%{${fg[red]}%}%B%45<...<%~%<<%b%{${fg[default]}%}'
prc[path]='%{${fg[cyan]}%}%1~%{${fg[default]}%}'
prc[lastresult]='%{${fg[red]}%}%?%{${fg[default]}%}%'
prc[timestamp]='%B%{${fg[blue]}%}[%T]%{${fg[default]}%}%b'
prc[user]='%{${fg[${usernamecolor}]}%}%n%}%{${fg[default]}%}'
prc[machine]='%{${fg[${hostnamecolor}]}%}${hostname}%}%{${fg[default]}%}'
prc[userspec]='%B%(!.%{${fg[red]}%}.%{${fg[green]}%})%n@%m%{${fg[default]}%}%b'

PROMPTOK="${prc[user]}@${prc[machine]} (${prc[path]})%# "
PROMPTERR="${prc[user]}@${prc[machine]} (${prc[path]},${prc[lastresult]})%# "

PROMPT="$PROMPTOK"
RPROMPT=""

# prompt for fixing typos
# from: http://rc.vc/files/zsh/dotzshrc
SPROMPT=$'Correct %{\e[31m%}%R%{ \e[0m%}to%{ \e[36m%}%r%{ \e[0m%}? [No/Yes/Abort/Edit]: '

################################################################################
autoload -Uz vcs_info

precmd () {
    local exitcode=$? ;

    if test "$exitcode" != "0"; then
	PROMPT=$PROMPTERR
    else
	PROMPT=$PROMPTOK
    fi
   
    # give me the git right-prompt when i'm in src
    # http://xana.scru.org/xana2/quanks/vcsinfoprompt/
    # get vcs info
    psvar=()
    vcs_info
    [[ -n $vcs_info_msg_0_ ]] && psvar[1]="$vcs_info_msg_0_"

    if [[ "$PWD" =~ "src" ]]; then
	RPROMPT="%(1v.%F{blue}%1v%f.) "
    else
	RPROMPT=""
    fi
    
    if test "$olddir" != "$PWD"; then
	case $TERM in
	    *xterm*)
		print -Pn "\e]0;%n@%M: %/\a"
		;;
	esac
    fi
    olddir="$PWD"
}
################################################################################

-- 
Dirk-Jan C. Binnema                  Helsinki, Finland
e:djcb@xxxxxxxxxxxxxxx           w:www.djcbsoftware.nl
pgp: D09C E664 897D 7D39 5047 A178 E96A C7A1 017D DA3C



Messages sorted by: Reverse Date, Date, Thread, Author