Tim Spurling

a promptly completed post

Opinions. Everyone has them.

One very popular thing to have an opinion about (if you are a nerd) is shell prompts.

Here is mine:


As you can see, it contains a fair bit of information. But how and/or why?!

Happily (but somewhat irrelevantly), I’m using the delightful (if stupidly-named) oh-my-zsh, which is quite a nice organization framework for zsh customization scripts. The effect of this, as far as this post is concerned, is that there is one file, ~/.oh-my-zsh/themes/tsprlng.zsh-theme, dedicated to this prompt:

As you can see, everything happens in the PROMPT variable, which is assigned a long and magical string; a gigantic and disorganized pile of special characters and escape sequences.

The most obvious recurring pattern is that for setting colours—take for example this bit: %{%(!.$fg[cyan].$fg[red])%}. This, a %(condition.ifTrue.ifFalse%) inside an %{escape%}, sets the colour of the following text to red if the shell has admin rights, or cyan otherwise—as is used for the path string.

The whole prompt is three lines (line breaks in the var result in real line breaks):

  • A blank line (for waste and clarity), or the (indented, bracketed, bold) status of the last command, if it failed.
  • Loads of stuff:
    • $(zsh_theme_ssh_prompt), the evaluated output of that function, which is a string to print the hostname in black on yellow iff you’re shelling over SSH. (This function could easily be extended to do different colours if you are Tom.)
    • ${(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]}), an enormous, illegible, brilliant trick I stole from my friend Anthony a couple of years ago and have been using since. It prints out a ‘short path string’ – the last two folders in full and all others truncated to the first letter.
    • $(git_prompt_info), which is an oh-my-zsh function for printing out git’s status when you’re in a repo folder. It prints out the branch name, surrounded by the contents of those four variables, which here prefix it with git: and suffix it with the stupid yellow ∆ (when necessary).
  • >: , which is yellow, unless you’re admin, in which case it’s red.

Bye! Next time, dotfiles. Maybe.

related posts

blog comments powered by Disqus