Today I’ll be talking about how to upgrade your tooling with an application that was released 25 years ago.
As a developer, you likely spend a good chunk of time using a shell in a terminal emulator. If you’re a Mac user and never updated your shell, you are likely using an old version of Bash from 2007 that ships with even the latest OSX. It’s not even the current major version. We can do better!
For Windows users that are following along, you’ll either need to use a vm/vps running some flavor of *nix, or cygwin. There is a Windows port, but it’s old and appears to be unmaintained.
What is Zsh?
Let’s just go over a few features.
$ ls -l **/*.js
What about files owned by you?
$ ls -l *(U)
One of my favorites is the alias functionality:
$ alias me='whoami' $ echo hello `me`
What about vi-style key bindings?
$ bindkey -v
In Bash, there is auto-completion. But in Zsh, it’s more advanced, and you can TAB to display and cycle forward, back or arrow through your options, then select that option without breaking to a new prompt.
Check out when I TAB mid-command:
$ git a add -- add file contents to index am -- apply patches from a mailbox apply -- apply patch to files and/or to index archimport -- import an Arch repository into git archive -- create archive of files from named tree
Sweet, path replacement.
~/long/path/app1/src/main/java/com/blah $ cd app1 app2 ~/long/path/app2/src/main/java/com/blah $
Substring filterting when arrowing through the history of commands? Yep. AND that history is shared across multiple terminals!
Check out some of these in action:
There’s so much more, but I’ll leave that for you to discover.
Are there other shells out there that are this fancy?
Zsh is not the only game in town. The Friendly Interactive Shell (Fish) is a more modern shell that, as its name implies, is great for interactive use. There are some Fish features that I like more than Zsh. But where Zsh wins for me is:
- Minus a few edge cases, the Zsh scripting language is backwards-compatible with Bash.
- Oh My Zsh is an awesome framework (there is also Oh My Fish, but it’s not as actively developed).
- One of most immediately satisfying features of Fish, syntax highlighting on the command line, is easily added as a Zsh plugin.
Everyone else on my team uses Bash. Won’t I run into compatibility issues?
Keep in mind that Zsh is mostly helping you out with interactive use of the command line. If you run a script using the full path, or a dot-slash with a relative path, it will respect the hashbang and run it the correct subshell. If you really need to be explicit, you can run
bash script.sh. If you need to use Bash interactively, just enter it by simply typing
bash and leave by typing
If you’re using a Mac, you should already have Zsh, but it’s probably old. Brew will help you. Linux folks, use your favorite package manager.
Get the Z Shell:
$ brew install zsh $ sudo pacman -S zsh $ sudo apt-get update && sudo apt-get install zsh $ sudo yum install zsh
Mac folks, you may need to edit your
etc/shells file by appending
/path/to/zsh to the list of shell paths. You can find this with
Next, we’ll grab Oh My Zsh, which is a configuration framework for the Z Shell. Trust me, you’ll want this. The default settings are already awesome, but we can do a few extra steps.
Install the Oh My Zsh framework:
$ curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh
Start a new terminal session or reboot.
If you don’t like it, then run the uninstall script:
Configuring Oh My Zsh
You may want to change themes. Here’s a bunch of screenshots to save you the trouble of setting each one up and trying it.
Ok, now crack open that
~/.zshrc file and change the value of
ZSH_THEME to the name of the theme you want. I’m partial to “muse”.
To browse a big ‘ol list of available plugins, run
ls ~/.oh-my-zsh/plugins. In your
~/.zshrc, go down to:
Leave in git, it’s far too valuable to remove. Just add whatever sounds clever separated by spaces. I don’t suggest going crazy and adding every plugin. Your shell would take forever to load. Do add some of the most appealing ones and try them out.
Possibly the most immediately useful plugin is zsh-syntax-highlighting. Valid commands are green, invalid ones are red. It’s like you’ve been eating plain toast all your life and this is the first time you’ve used butter. It doesn’t come bundled with Oh My Zsh, so we’ll need to install it as a custom plugin.
Create a folder for custom plugins.
$ mkdir ~/.oh-my-zsh/custom/plugins
$ git clone git://github.com/zsh-users/zsh-syntax-highlighting.git
~/.zshrc, add the plugin
$ plugins=(git zsh-syntax-highlighting)
If you want to see what aliases you already have, type
Nice. You may want to add a few more.
~/.oh-my-zsh/custom/, create and open a file with whatever name you want, like
aliases.zsh. The first thing I recommend adding to this file:
alias reload=". ~/.zshrc && echo 'Your zshrc file has been reloaded'"
From now on, any time you want to change a configuration or add a custom script, just type
to source your config file, which will update your session with any changes you have made.
Here’s a few aliases I like:
List all git repos in home directory.
alias repos="find ~ -type d -name .git | xargs -n 1 dirname"
Sychronize and update all packages (use your package manager):
alias update="yaourt -Syua"
That’s all for now. Go forth with your new shell and prosper!
— Joe Post, firstname.lastname@example.org