zsh prompt

I’ve just spent an inordinate amount of time writing a zsh prompt:

export PROMPT='%{%(!.$fg[red].$fg[yellow])%}${(r:1:)${(%):-%~}}$([
[ ${(r:1:)${(%):-%~}} != "/" && ${#${(%):-%~}} -gt 1 ]] && echo /)
$([[ ${#${(@s:/:)${(%):-%~/a}[2,-1]}} -gt 3 ]] && echo ${(j:/:)${(
@r:1:)${(s:/:)${(%):-%~}[2,-1]}[(ws:/:)1,(ws:/:)-3]}}/)$([[ ${#${(
@s:/:)${(%):-%~/a}[2,-1]}} -gt 2 ]] && echo ${(@j:/:)${(s:/:)${(%)
:-%~}[2,-1]}[-2,-1]})$([[ ${#${(@s:/:)${(%):-%~/a}[2,-1]}} -eq 2 ]
] && echo ${${(s:/:)${(%):-%~}}[-1]})%{%(!.$fg[yellow].$fg[red])%}
%(?..%B(%?%)%b)%{$fg[default]%}$ '
export RPROMPT="[%{$fg[yellow]%}%*%{$fg[default]%} on %{$fg[yellow
]%}%D%{$fg[default]%}]"
(Remove the line breaks)

Basically, if you put this code in your .zshrc you get a prompt like this (although with better colours):

~/Anthony/Work$ cd imperial/261\ Lab\ 2                                  [18:31:58 on 10-01-30]
~/A/W/imperial/261 Lab 2$ perl -e 'exit 63'                              [18:32:09 on 10-01-30]
~/A/W/imperial/261 Lab 2(63)$                                            [18:33:10 on 10-01-30]

On the left is the current working directory, the exit code of the last command (if not 0), and a $
On the right is the time and date.

The path on the left side abbreviates all but the last two path elements to a single letter (and a lot of esoteric code was required to get this working).

Note: to do this, you need a few extra lines in your .zshrc to set things up:

autoload colors zsh/terminfo
colors
setopt prompt_subst

Have fun editing your prompt line!

autoload colors zsh/terminfo
colors
setoptĀ autoload colors zsh/terminfo
colors
setopt prompt_subst

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.