Give Zsh a Chance

I’ve been hearing a bit of buzz around zsh as a “better bash” for quite a few years now, and finally decided to give it a go as my shell.

It certainly seems fine so far, but doesn’t feel like the quantum leap from dark ages to rennaissance that the hype would suggest.

In this post I’ll talk about why people are going on about it, and how to set it up.

A quick google for zsh will give you lots of posts telling you that it’s the best thing since sliced arrays. A couple that I read were:

What are the touted features?

  • powerful auto-completion
  • autocorrect
  • themeable prompts
  • shared command history
  • globbing on steroids
  • vi and emacs editing modes
  • syntax highlighting

None of that gets me really fired up, given you can get it all in bash (mostly). But the resounding stance of most commenters seems to be that Zsh does it all a lot better. The prospect of a decent bit of vi editing at the command prompt certainly got me a little piqued (you can do it in bash, but I think zsh can take it a bit further.)

So I figure I’ll give it a go.

Setting up

Step 1: get Zsh.

1
$ sudo apt-get install zsh

Job done!

Well actually, not quite.

oh-my-zsh and prezto

One thing you need to figure out is which super-duper community maintained collection of configurations and modules you should use. (Presumably you could just rock along with plain vanilla Zsh, but using one of these projects seems to be the recommendation.)

oh-my-zsh was first on the block, and kickstarted a lot of the recent interest in Zsh I believe. prezto pretty much does the same thing as oh-my-zsh, but a few comments here and there seem to suggest that it improves on slow start up times of oh-my-zsh.

I haven’t really looked into it, so oh-my-zsh might be just as fast now for all I know. I just decided to go with prezto.

Hey prezto

As per the prezto readme on github, here’s how I set prezto up:

1
2
$ zsh
$ git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"

As far as I can tell ${ZDOTDIR} isn’t set by default, so .zprezto will go into your home dir unless you explicitly set it first.

zprezto comes with a bunch of predefined config files for zsh, which you need to add an alias for. You can use this groovy script:

1
2
3
4
setopt EXTENDED_GLOB
for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
  ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
done

Or just do it manually on the relevant files in .zpresto/runcoms, like:

1
2
3
4
$ cd
$ ln -s .zprezto/runcoms/zshrc .zshrc
etc
etc

And set zsh as your default shell:

1
$ chsh -s /bin/zsh

Tweaking prezto

Themes

You can twiddle about with the look of the zsh prompt to your heart’s content. There are also a bunch of premade themes that you can use that are included in the box. You can preview them with:

1
prompt -p <tab>

and then set the theme in your .zpreztorc if you like it. I tried a few and then just stuck with the default of ‘sorin’. Lots of people like to make their own, so you can find plenty floating around out there on the internet.

Modules

prezto gives you a bunch of modules to extend the zsh functionality. I presume the prezto modules are wrappers around existing zsh modules. They’re not all on by default, so you have to include the extras that you want. I added git, and syntax-highlighting.

1
2
3
4
5
6
7
8
9
10
11
12
zstyle ':prezto:load' pmodule \
  'environment' \
  'terminal' \
  'editor' \
  'history' \
  'directory' \
  'spectrum' \
  'utility' \
  'git' \
  'syntax-highlighting' \
  'completion' \
  'prompt'

syntax-highlighting didn’t work properly at first. If you uncomment the example in .zpreztorc:

1
2
3
4
5
# Set syntax highlighting styles.
zstyle ':prezto:module:syntax-highlighting' styles \
 'builtin' 'bg=blue' \
 'command' 'bg=blue' \
 'function' 'bg=blue'

You get an error along the lines of:

1
/home/foo/.zprezto/modules/syntax-highlighting/init.zsh:26: command not found: bg=blue

I was able to resolve this by editing lines 26 and 27 in .zprezto/modules/syntax-highlighting/init.zsh and concatenating them into one:

1
ZSH_HIGHLIGHT_STYLES[$syntax_highlighting_style]= "$syntax_highlighting_styles[$syntax_highlighting_style]"

UPDATE: I put in a pull request and this issue is fixed in the prezto sources.

I didn’t really like the defaults so I then set up the following highlights:

1
2
3
4
5
6
zstyle ':prezto:module:syntax-highlighting' styles \
  'builtin' 'fg=118' \
  'command' 'fg=118' \
  'alias' 'fg=118' \
  'path' 'fg=white,bold' \
  'function' 'fg=118'

tmux

Last bit for now was getting it working with tmux. tmux can be a bit funky getting 256 colours. I’m not using the tmux module as of yet, so I added something to .zshrc. For reasons I can’t remember right now, I had added the following to my bash config:

1
[ -z "$TMUX" ] && export TERM=xterm-256color

I put it in .zshrc and got all my nice colours when running tmux from within zsh.

z-ok

So zsh seems nice, but so far I’m not getting a teary-eyed feeling of joy like I did when I set up tmux. I do like the syntax highlighting, but am not overwhelmed by anything else so far. I’ll stick with it though; I’m hoping that the full power of zsh will be revealed over time.

Comments