6

When working on a git repository with multiple branches, how do I modify my zsh prompt to include my current active branch?

3

Use the vcs_info function in the zsh user contributions (included in the zsh package). Quick start:

autoload -Uz vcs_info
precmd () { vcs_info }
setopt prompt_subst
PS1="\$vcs_info_msg_0_$PS1"

It's likely that you'll want to make the output prettier. Since that's matter of personal taste, I refer you to the examples in the documentation.

3

Use oh-my-zsh. It is an auto-updating collection of several dozen plugins and themes that make zsh even better than it is already. Assuming that you have git installed already, it will automatically activate the git plugin, and then you're ready to go!

1

For those wanting something more lightweight, here's my heavily modified Zsh port of the bash prompt Gentoo came with by default when I was on it.

function parse_git_branch() {
    # Speed up opening up a new terminal tab by not
    # checking $HOME... which can't be a repo anyway
    [ "$PWD" = "$HOME" ] && return

    # Fastest way I know to check the current branch name
    ref="$(command git symbolic-ref --short HEAD 2> /dev/null)" || return
    echo " [$ref]"
}

prompt_gentoo_precmd() {
    path_prompt="%B%F{blue}%1~$(parse_git_branch)"

    PS1="$base_prompt$path_prompt $pre_prompt%# $post_prompt"
    PS2="$base_prompt$path_prompt $pre_prompt%_> $post_prompt"
    PS3="$base_prompt$path_prompt $pre_prompt?# $post_prompt"
}

prompt_gentoo_setup () {
    base_prompt="%k%B%(!.%F{red}.%F{green}%n@)%m "
    pre_prompt="%(0?..%F{yellow})%(1j.%%.)"
    post_prompt="%b%f%k"

    precmd_functions+='prompt_gentoo_precmd'
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.