Shell game with zsh

The default Macintosh shell is bash. It was the default shell in all the Linux distributions I ever used, and is, as far as I know, still the default shell in those distributions. I feel comfortable using it, but I was always jealous of one feature in zsh: the right prompt.

You probably know that bash has an environment variable, PS1, that controls the prompt string. There are lots of clever things you can do with PS1: have it display the date, the time, the current directory, the command history number, the user name, the hostname of the computer, etc. I always had it display the current directory--not the full pathname, just the last item in the path. This was a compromise. Because I have many directories with the same name (“report” seems to be the most common), I wanted to see the full pathname, but that would often take up so much space there wouldn’t be much room left on the line for the command itself. Bash will happily wrap the command as you type, but it’s harder to read a wrapped command.

Hence my zsh jealousy. Zsh, like bash, has a PS1 environment variable, but it also has an RPS1, which controls a right-justified string that appears on the command line but magically vanishes as the command you’re typing approaches it. There’s no need to worry about how long RPS1 is, because it doesn’t get in the way of your command.

That, at least, was the promise of zsh. For some reason, though, I was never able to get the zsh prompts configured right on any of my Linux machines. I tried making it my default shell a few times but always ran into something that I couldn’t overcome and went back meekly to bash.

Today, however, I can tentatively announce victory over zsh’s configuration requirements. My ~/.zshrc file has these lines

# set the prompts
autoload -U colors && colors
PS1="%{${fg[blue]}%}%# %{${fg[black]}%}"
RPS1="%{${fg[red]}%}%~%{${fg[black]}%}"

which give me a Terminal that looks like this (click on the image to see it full-sized).

The autoload sets up the fg associative array so the prompt colors can be called by name rather than messing with terminal escape codes. The %# in PS1 shows the percent sign prompt if you’re a regular user and a hash sign (#) if you’re the root user. The %~ in RPS1 shows the path name of the current working directory, shortening your home directory to ~.

Changing your shell on a Macintosh is a little weird. Open System Preferences and select Accounts. Make sure the lock in the lower left corner of the Accounts pane is unlocked, then Control-click on your user name (or picture) to bring up the context menu. Choose Advanced Options… from the menu and then change the Login Shell in the Advanced Options sheet. I remember Linux as being more straightforward.

Generally speaking, zsh works the same as bash. It has more ways of doing completions, but my bash habits seem to work. One difference I quickly discovered: zsh does not have a HOSTNAME environment variable; it uses HOST instead. When I started using zsh, I copied most of my .bashrc file into .zshrc. One of the lines relied on HOSTNAME and failed because there is no HOSTNAME. After change it to HOST everything went smoothly.

Tags: