Friday, August 31, 2012

My zsh prompt string PS1

Every Unix user with a blog has a post about it: how they have configured their PS1 so their command prompt is almost a mini dashboard that shows everything they’d need to know.  I am no exception.  Even if this post doesn’t really help others, bragging is gratifying, so I’d go on and show how awesome my PS1 is :)

I have been using zsh for a while now, and I am quite happy with it.  I’m a sucker for colours and I was annoyed that I couldn’t take full advantage of my 256 colour terminal because I just didn’t know how to.  So I searched the web and found two good pages.  Combined with some manual-reading I had done, I cooked up my shiny new PS1:
PS1='%(?.%F{245}.%F{124})%2m %F{130}%~ %F{215}%# %f'
Unfortunately, prompt strings are never intuitive, so I’ll do some explaining.
  • %F{colour_code} sets the foreground colour to the specified colour.  Colour code is simply a number in the range 0 to 255 (assuming your terminal supports 256 colours).
  • %f resets the foreground colour to default.
  • %(condn.true_string.false_string) is a conditional expression.  If condn evaluates to true, true_string is emitted; otherwise false_string is emitted.  Like you’d expect, you can have some special strings in condn to mean interesting things.
Deconstructing my PS1
I actually started with something more simple:
PS1='%F{245}%2m %F{130}%~ %F{215}%# %f'
The prompt string starts with the machine name printed in grey.  Then comes the current working directory in a yellow/orange colour, followed by zsh’s % prompt that’s a wee bit brighter than the directory name.  But I wanted to have the exit status of the previous command in my prompt somewhere.  It’s not some information I really need, but I thought it can be a nice-to-have.  But at the same time I didn’t want this information to clutter up my prompt.

I figured I could use make use of colours to differentiate between success and failure of the previous command.  So I changed %F{245}%2m to %(?.%F{245}.%F{124})%2m.  Now, the machine name is printed in grey if the previous command succeeded, and in red if it had failed.  Thus born the prompt string that exactly fits my needs... for now.

Friday, August 10, 2012

Only 8 colours on ssh-ed terminals?

Vim (i.e. console vim, but not gvim) on my primary workstation uses 256 colours and with syntax highlighting on, editing source code is a much pleasant experience (vs. being limited to 8 colours).

However, every time I ssh a different machine, Vim on the remote machine always used 8 colours.  I’m a big fan of seeing a lot of colours and hated the 8-colours environment of the remote machines.  After some digging, I found out the cause for this problem.  My primary machine has COLORFGBG environment variable set, but the remote machines don’t.  Check Skåne Sjælland Linux User Group to see how you can fix this issue.  (Essentially, you’ll have to configure ssh on your machine and the remote machine to pass COLORFGBG environment variable.)

Update: Actually, adding the line set background=dark to my ~/.vimrc seems like a much easier solution.  And also, “only 8 colours” is not the real problem.  Real problem is that vim is using a different set of colours (dark blue instead of light blue, for instance) on the new machine.