Lately, I’ve been plagued with getting my zsh prompt exactly the way I want. I’ve been using the clint theme bundled with zsh, but it’s multiline prompt, and poor color choice started getting to me. So, I figured I’d change it and start with something simple, maybe mimicking bash a little, and slowly building out from there. The result, thus far, I’m pleased with. It’s a basic bash-like prompt, showing the username, hostname, current working directory and current history number. What isn’t showing, but will if needed, is the returned exit code on failure. Here’s a demo of the prompt in action:
aaron@kratos:~ 262 % ls -ld tmp drwxrwxr-x 2 aaron root 4096 2008-01-30 13:22 tmp aaron@kratos:~ 263 % rm -rf tmp aaron@kratos:~ 264 % ls -ld tmp ls: tmp: No such file or directory aaron@kratos:~ 265 % echo $? 2 aaron@kratos:~ 266 %
Nice. Only showing the exit code, when there’s a failure, otherwise, keep it hidden. What I currently can’t figure out, but will, is showing only when I’m behind a screen session. So far, the PS1 variable is as follows:
aaron@kratos:~ 266 % echo $PS1 %n@%m:%~%(?..[%?]) %h %#
%n - username %m - hostame up to the first dot %~ - current working directory %(?..%?) - testing for failure on exit code %? - the exit code %h - history number %# - % if unprivileged, # if privileged
The tricky part for me, was getting the syntax for testing the exit code. The syntax is %(x.true-string.false-string), where ‘x’ is some arbitrary testing condition, in this case, ‘?’ for exit codes. After the first dot, if the test passes, show true-string, if it fails, show false-string after the second dot. So, in my case, I’m returning an empty string on a returned 0 exit code, and the failed exit code on false (in brackets). Now, to figure that out with screen sessions.
What do you think? If you’re a zsh user, I’d be interested in your PS1 variable.