zsh prompt, part 2

June 29, 2008

linux

Every now and then I get the ‘prompt-itch’ and then I need to tweak my prompt again :) Of course I’m very happy with the way my prompt was.

My idea of a good prompt is to be as short as possible and still be informative about your environment:

Only tell stuff I’m really interrested in and for the rest SHUT THE HELL UP!

So no date output in my prompt, If I want to know the time I will type date myself. I’m still thinking about leaving out the current hostname and current username… but for some reason I’ve become attached to seeing elektron (my hostname) on the screen.

But a prompt can always be done better. So first a screenshot with some comments about my new prompt (btw: what a lousy jpg, but I’m in no mood to make a better one)

screenie of my new zsh prompt

A few things have changed in this prompt:

  • shorter username, show only the first character. Why do you need more?
  • use $SSH_TTY to tell if this is remote login or not, if so print a @
  • use signal names when the exit code indicates that the prev. command received one. So no more 130, but -INT.

short username and ssh

I will only document the actual changes in this prompt, for the shorter username I use: ${USER[1]}, which yields the first character.

For ssh I use:

if [ -z $SSH_TTY ]; then
    ZSSH=
else
    ZSSH='@ '
fi

Now the left side of the prompt becomes:

ZU=${USER[1]}
PS1=$'$C_BLUE%(1j.$myjobs% $C_OFF .$C_OFF)%B$ZSSH%b%m.%B$ZU%b$C_OFF$C_L_GREEN%#$C_OFF '

signal names My right side prompt shows the exit code of the previous command, but of course only when it is not 0. I’ve extended this to show the signals names in case the exit code is larger than 127.

setup the signal names and put them in an array:

ZSIG=$(kill -l)

And in precmd() we index in the $ZSIG array to pinpoint the correct signal name using the exit code. This is the prepended with a - to mimic the kill command syntax.

# add the name of the signal if its there
# must be done here in this function... 
EX=`print -P %?`
# EX=$(print -P %?) # does not work...
if [[ $EX -ge 128 ]]; then
    ((k=$EX-128))       # calculate the index in ZSIG
    E="-${${(s: :)ZSIG}[$k]}"  # split ZSIG on space and index with $k
else
    E=$EX
fi

The complete right side of the prompt then becomes:

RPSR=$'$C_OFF$C_RED%(0?.$C_OFF. $E)$C_OFF'

The full file can be downloaded here

None