Moving to zsh
October 21st, 2007
I'm working my way through From Bash to the Z Shell. I thought it was time I became a command-line guru because I'm sure I'm missing valuable tricks.
What I've found out so far about zsh:
- it comes with loads of completions built in (even one for darcs!)
- it's got a clever typo corrector
I'm only at chapter 2 but I've skipped ahead a bit. So far all I've been able to do is make the prompt display a fixed number of path segments (much cleaner than fixed-width truncation):
27> ~ % cd Documents
28> ~/Documents % cd Development
29> ~/Documents/Development % cd bdd
30> ~/Documents/Development/bdd % cd rspec-trunk
31> ../Documents/Development/bdd/rspec-trunk % cd rspec
32> ../Development/bdd/rspec-trunk/rspec %
UPDATE: threw away my 12 line precmd function and used the ternary operator described by David Porter in the comments below. Ironically, in From Bash to the Z Shell, the section on the ternary operator comes right before (p153) the section on path truncation (p155) and yet the path segments test is mentioned in neither.
Here is my current ~/.zshrc:
export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH
export EDITOR="mate -w"
export SVN_EDITOR="mate -w"
alias nano="nano -w"
# correct typos in command names
setopt correct
# load completions
autoload -U compinit
compinit
# needed to get variable substitutions in prompts
setopt prompt_subst
# [line number] [truncated path] %
PS1='%!> %(5~|../|)%4~ %# '

November 4th, 2007 at 12:04 AM
Hi Ashley,
zsh provides a ternary expression prompt sequence that can be used to set the path prefix easily:
PS1='%!> %(5~|../|)%4~ %# '
Hope that's useful.
November 4th, 2007 at 12:15 PM
Hi David
That works a treat :) Not only is it about 10x shorter but it works properly in the case of paths not descending from ~. (Mine did something weird like ..//opt/local/lib/ruby)
I'll update the article