Skip to content

My ZSH Prompt Improved

I’ve been meaning to get to this for some time, but haven’t gotten around to it until today. In a previous post, I shared with the world my zsh PS1 variable. Well, I extended it a bit this morning making it more informative. First, I need to setup a scenario:

I’m running screen locally on my laptop (we’ll refer to it as SCREEN_L), and remotely on my server (we’ll refer to this one as SCREEN_R). For SCREEN_L, there is a ~/.screenrc that I use to show what screen window buffer I’m using via the “hardstatus” directive. My ~/.screenrc is thus:

aaron@kratos:~ 2609 % cat .screenrc
screen -t python 0
screen -t irssi 1
screen -t shell 2
screen -t notify 3
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m %{W}%c %{g}]' 

That puts a nice status line at the bottom of my terminal, letting me know what screen buffer I’m in, as well as it’s title. However, I don’t want to run a ~/.screenrc under SCREEN_R. This just means yet another hardstatus line that I really don’t want (I value my pixels). Rather, I would like to be notified what screen window I’m under, if any, in my prompt. After a bit of digging, I came up with the solution.

First, the environment variable $WINDOW is keeping track of this automatically for me. For example:

aaron@kratos:~ 2610 % echo "\"$WINDOW\""
""
aaron@kratos:~ 2611 % screen
aaron@kratos:~ 2612 % echo "\"$WINDOW\""
"0"

Perfect! Now, to get it into my prompt. This is easy with adding a little if logic to our ~/.zshrc:

if [ x"$WINDOW" = x ]; then
    SCREEN="($WINDOW)"
else
    SCREEN=""
fi
PS1="%n@%m$SCREEN:%~%(?..[%?]) %h %# "

What am I doing here? Well, first off I’m checking to see if the $WINDOW variable is blank. I’m using x”$WINDOW” != x for a simple reason. Zsh is expecting a valid condition after ‘!=’. Unfortunately, an empty string isn’t satisfying it. So, I prepend the letter x to the $WINDOW variable, and test to make sure it doesn’t equal just ‘x’. Second, if $WINDOW equals anything other than ‘x’, a SCREEN variable is defined with the $WINDOW variable contents wrapped in parenthesis. This $SCREEN variable is then added to the prompt.

Now, I add this to my ~/.zshrc for my SCREEN_R, and I can see where I’m at in my screen session remotely without the need for a remote ~/.screenrc. Here’s the results, also showing off exit code:

aaron@kratos:~ 2614 % screen
aaron@kratos(0):~ 2615 % ping foo
ping: unknown host foo
aaron@kratos(0):~[2] 2616 %

Cheers!

{ 2 } Comments

  1. Christoph using Safari Safari 525.13 on Mac OS Mac OS X | April 9, 2008 at 10:30 pm | Permalink

    Why don’t you use [[ -n $WINDOW ]]; instead of using this x$WINDOW != x trick?

  2. Aaron using Mozilla Firefox Mozilla Firefox 2.0.0.13 on Ubuntu Linux Ubuntu Linux | April 16, 2008 at 11:22 am | Permalink

    @Christoph- Because I’m a horrible script writer, still learning the ways of scripting Zen.

Post a Comment

Your email is never published nor shared. Required fields are marked *

Powered by WP Hashcash