Inspired by Brian Carper's recently published Zsh prompt as well as Steve Losh's, I decided it was high time I roll my own.

I wanted to have a not too flashy two-liner with just enough information and styling. As an added bonus, it detects if the user is root or not and applies some simple changes in style to warn the user.

About how the Git (and other VCS) information works you should check Brian's post, since I shamelessly borrowed it from him. My main adition to his system was incorporating (and extanding) Steve Losh's VCS detection system.

I spent quite some time RTFM'ing and asking on #Zsh to figure out how the colouring and styling works. So I'm fairly sure the %F{…}…%f is the preferred way of handling it. Also I used ANSI colours exclusively, so the colour schemes of the terminal (in my case Konsole) can handle them correctly.

Note that the date and time in RPROMPT are localised to my country. You should find it easy to adapt it if needed, I hope.

Here's the obligatory screenshot:

Screenshot of a very cool Zsh prompt

In root's case everything looks the same, only the username (i.e. "root") is bold and underscored; and the prompt sign (#,±,☿ or ⚡ in this case) is coloured red.

And here's the code:

### Shows state of the Versioning Control System (e.g. Git, Subversion, Mercurial
autoload -Uz vcs_info

zstyle ':vcs_info:*' stagedstr '%F{green}●%f'
zstyle ':vcs_info:*' unstagedstr '%F{yellow}●%f'
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat '%b%F{red}:%f%F{yellow}%r%f'
zstyle ':vcs_info:*' enable git svn
precmd () {
    if [[ -z $(git ls-files --other --exclude-standard 2> /dev/null) ]] {
        zstyle ':vcs_info:*' formats '%F{cyan}[%b%c%u%f%F{cyan}]%f'
    } else {
        zstyle ':vcs_info:*' formats '%F{cyan}[%b%c%u%f%F{red}●%f%F{cyan}]%f'


### Detects the VCS and shows the appropriate sign
function prompt_char {
    git branch >/dev/null 2>/dev/null && echo '±' && return
    hg root >/dev/null 2>/dev/null && echo '☿' && return
    svn info >/dev/null 2>/dev/null && echo '⚡' && return
    echo '%#'

### Needed for a pretty prompt
setopt prompt_subst # Enables additional prompt extentions
autoload -U colors && colors    # Enables colours

### My default prompt
PROMPT='%(!.%B%U%F{blue}%n%f%u%b.%F{blue}%n%f) at %F{magenta}%m%f on %F{yellow}%y%f in %F{cyan}%~%f
{${vcs_info_msg_0_} %(!.%F{red}$(prompt_char)%f.$(prompt_char)) }: %{$reset_color%}'
### My default prompt's right side
RPROMPT='%F{cyan}%D{%e.%b.%y %H.%M}%f%{$reset_color%}'

### My prompt for loops
PROMPT2='{%_}  '

### My prompt for selections
PROMPT3='{ … }  '

### So far I don't use "setopt xtrace", so I don't need this prompt

…which you can also obtain as part of my Zsh dotfiles repository.

hook out → feeling a lot less burdened, but still quite a bit of backlog to tackle…

P.S. I think this is the first ever published screenshot on Hook's Humble Homepage (since 2003). Boy, I'm pushing my boundries lately ;)

Related Posts