{"id":16,"date":"2010-01-30T18:34:29","date_gmt":"2010-01-30T18:34:29","guid":{"rendered":"http:\/\/81.86.147.124:7564\/blog\/?p=16"},"modified":"2010-03-08T02:20:09","modified_gmt":"2010-03-08T02:20:09","slug":"zsh-prompt","status":"publish","type":"post","link":"https:\/\/bryants.eu\/blog\/2010\/01\/zsh-prompt\/","title":{"rendered":"zsh prompt"},"content":{"rendered":"<p>I&#8217;ve just spent an inordinate amount of time writing a zsh prompt:<\/p>\n<pre style=\"font: normal normal normal 12px\/18px Consolas, Monaco, 'Courier New', Courier, monospace;\">export PROMPT='%{%(!.$fg[red].$fg[yellow])%}${(r:1:)${(%):-%~}}$([\r\n[ ${(r:1:)${(%):-%~}} != \"\/\" &amp;&amp; ${#${(%):-%~}} -gt 1 ]] &amp;&amp; echo \/)\r\n$([[ ${#${(@s:\/:)${(%):-%~\/a}[2,-1]}} -gt 3 ]] &amp;&amp; echo ${(j:\/:)${(\r\n@r:1:)${(s:\/:)${(%):-%~}[2,-1]}[(ws:\/:)1,(ws:\/:)-3]}}\/)$([[ ${#${(\r\n@s:\/:)${(%):-%~\/a}[2,-1]}} -gt 2 ]] &amp;&amp; echo ${(@j:\/:)${(s:\/:)${(%)\r\n:-%~}[2,-1]}[-2,-1]})$([[ ${#${(@s:\/:)${(%):-%~\/a}[2,-1]}} -eq 2 ]\r\n] &amp;&amp; echo ${${(s:\/:)${(%):-%~}}[-1]})%{%(!.$fg[yellow].$fg[red])%}\r\n%(?..%B(%?%)%b)%{$fg[default]%}$ '\r\nexport RPROMPT=\"[%{$fg[yellow]%}%*%{$fg[default]%} on %{$fg[yellow\r\n]%}%D%{$fg[default]%}]\"\r\n(Remove the line breaks)<\/pre>\n<p>Basically, if you put this code in your .zshrc you get a prompt like this (although with better colours):<\/p>\n<pre>~\/Anthony\/Work$ cd imperial\/261\\ Lab\\ 2                                  [18:31:58 on 10-01-30]\r\n~\/A\/W\/imperial\/261 Lab 2$ perl -e 'exit 63'                              [18:32:09 on 10-01-30]\r\n~\/A\/W\/imperial\/261 Lab 2(63)$                                            [18:33:10 on 10-01-30]<\/pre>\n<p>On the left is the current working directory, the exit code of the last command (if not 0), and a $<br \/>\nOn the right is the time and date.<\/p>\n<p>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).<\/p>\n<p>Note: to do this, you need a few extra lines in your .zshrc to set things up:<\/p>\n<pre>autoload colors zsh\/terminfo\r\ncolors\r\nsetopt prompt_subst<\/pre>\n<p>Have fun editing your prompt line!<\/p>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">autoload colors zsh\/terminfo<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">colors<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">setopt\u00a0autoload colors zsh\/terminfo<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">colors<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">setopt prompt_subst<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve just spent an inordinate amount of time writing a zsh prompt: export PROMPT=&#8217;%{%(!.$fg[red].$fg[yellow])%}${(r:1:)${(%):-%~}}$([ [ ${(r:1:)${(%):-%~}} != &#8220;\/&#8221; &amp;&amp; ${#${(%):-%~}} -gt 1 ]] &amp;&amp; echo \/) $([[ ${#${(@s:\/:)${(%):-%~\/a}[2,-1]}} -gt 3 ]] &amp;&amp; echo ${(j:\/:)${( @r:1:)${(s:\/:)${(%):-%~}[2,-1]}[(ws:\/:)1,(ws:\/:)-3]}}\/)$([[ ${#${( @s:\/:)${(%):-%~\/a}[2,-1]}} -gt 2 ]] &amp;&amp; echo ${(@j:\/:)${(s:\/:)${(%) :-%~}[2,-1]}[-2,-1]})$([[ ${#${(@s:\/:)${(%):-%~\/a}[2,-1]}} -eq 2 ] ] &amp;&amp; echo ${${(s:\/:)${(%):-%~}}[-1]})%{%(!.$fg[yellow].$fg[red])%} %(?..%B(%?%)%b)%{$fg[default]%}$ &#8216; export [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/posts\/16"}],"collection":[{"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":4,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":18,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/posts\/16\/revisions\/18"}],"wp:attachment":[{"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}