10 super powers for your shell

Still using Bash? It's cool, though you might want to switch to zsh and give your shell some super powers! Give it a shot, you'll love this!

You might have been advised elsewhere to use the zsh-newuser-install but I wouldn't recommend that. While the intentions of this little program are very good, it scared me off from switching to zsh in the past. There is a much better way thanks to a community-driven zsh framework oh-my-zsh. This wonderful framework will configure your zsh with super powers collectively build based on experience of the community. Not only that, it will allow you to select more esoteric super powers as plugins. Even better, it will allow you to select one of more than hundred themes that delivers super powers to your prompt. Excited? Read on.

Installation

Oh-my-zsh is trivial to install, on Arch Linux you can do


# Arch Linux with yaourt to fetch & install from AUR
yaourt -S oh-my-zsh-git
# Copy template zshrc to your home directory to enable oh-my-zsh for current user
cp /usr/share/oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
# Switch current session to zsh
zsh
# Ready to commit? Change your default shell to zsh:
chsh -s /bin/zsh

Or if your system doesn't come with package manager capable of installing oh-my-zsh then you can do:


# Any system with curl, git and zsh
curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh

provided that you have git and zsh installed (obviously). However you probably shouldn't run random scripts of the internet without reading it first. This scripts clones oh-my-zsh repo into a hidden directory in your home, configures zsh to use oh-my-zsh, and subsequently changes your default shell and switches your current shell to zsh.

Either way you should now see very basic but colorful prompt. Let's try the magic powers!

SuperPower 1: Command completion


# I can never remember nmap switches. No more need for man nmap!
nmap -‹tab›‹tab›↓
zsh showing nmap suggestions
Oh my zsh showing nmap suggestions

See that -P is highlighted? Navigate options with ↑ and ↓ and select one using ↵.


# I dont need no MC!
/‹tab›‹tab›↓→
zsh showing filesystem suggestions
Oh my zsh showing filesystem suggestions

SuperPower 3: Known hosts

Zsh will suggest usernames & hosts based on where you've previously sshed to.


# Let's ssh to that other machine in 
ssh ro‹tab›10‹tab›‹tab›‹tab›↓→
zsh suggesting hosts you've previously logged in to
zsh suggesting hosts you've previously logged in to

SuperPower 4: Remote scp suggestions

Zsh will list remote directory and/or matching files when using scp. It will only work if you've password-less access to the remote, i.e. when you've pre-shared rsa/dsa keys.


scp root@192.168.2.20:‹tab›‹tab›↓→
zsh suggesting remote files for scp
zsh suggesting remote files for scp

SuperPower 5: Kill suggests processes to kill


# is your chrome misbehaving?
kill chr‹tab›↓↓
zsh giving you processes name for kill
zsh giving you processes name for kill

SuperPower 6: Automatically correct typos

Epsecially useful when you make typos as oftn as I do.


mkdir new-project
# surely fast hatchback is not what you wanted!
gti init
zsh autocorrects a typo
zsh autocorrects a typo

As a matter of fact oh-my-zsh comes with some exceptions built-in

However it can be bit too overzealous, e.g. I happened to have folder .config in my home directory:


# auto-correction is cool! Let's enable it for git commands as well!
git config --global help.autocorrect 1
zsh overzealous autocorrection
zsh overzealous autocorrection

If you find a case that is particularly annoying you can add exceptions by aliasing a command with one prefixed 'nocorrect':


alias git='nocorrect git'

SuperPower 7: Prompt magic

Wouldn't it be cool to have git repo name/branch/status into your prompt? Now you can:

git repo info in your prompt
git repo info in your prompt

There are more than 100 themes in core repository and then there are thousands of forks if you can be bothered to browse them. Surely at least one of these will match your needs. And if not, oh-my-zsh comes with predefined functions to help you build your own!

Start typing and press ↑ and ↓. Zsh will search for any command containing that string. to enable this functionality you need to enable plugin history-substring-search.


feature↑↑↑
zsh search history
zsh search history

SuperPower 9: Syntax highlighting

This one is a bit more involved as you need to install additional plugin.


# Arch users can obtain this plugin from AUR
yaourt -S zsh-syntax-highlighting 
# We need to 'integrate' with oh-my-zsh
mkdir -p ~/.oh-my-zsh/custom/plugins
cd ~/.oh-my-zsh/custom/plugins
ln -s /usr/share/zsh/plugins/zsh-syntax-highlighting .

# if you're not running Arch Linux you can clone repository in oh-my-zsh instead:
cd ~/.oh-my-zsh/custom/plugins
git clone git://github.com/zsh-users/zsh-syntax-highlighting.git

In either case you'll need to update your zsh config file ~/.zshrc where the following two entries are required before final source $ZSH/oh-my-zsh.sh call:


# define where custom plugins live:
ZSH_CUSTOM=~/.oh-my-zsh/custom/
# Make sure plugins section contains zsh-syntax-highlighting:
plugins=(history-substring-search zsh-syntax-highlighting)

And this is how it looks like:

zsh giving you processes name for kill
zsh giving you processes name for kill

SuperPower 10: Integration with FASD (or autojump)

Fasd is a command-line productivity booster inspired by tools like autojump, z and v.

To get fasd working we need to install it. You might need to do it manually unless your system package manager contains fasd.


# Arch Linux users are in luck
yaourt -Sy fasd

Add the following line somewhere at the end of your zsh config file:


eval "$(fasd --init auto)"

Fasd comes with pretty epic set of features beyond the scope of this post; if you get used to it, it will greatly boost your productivity:

zsh with FASD workflow
zsh with FASD workflow

Also it nicely integrates with your zsh so you get all expected feautes when it comes to auto completion:


z lib‹tab›→
zsh with FASD completion
zsh with FASD completion

If installing fasd is too much hassle for you might have more luck with autojump - once you have it installed you can use plugin bundled with oh-my-zsh.

More shell completions

Out-of-the-box zsh come with quite completion support for common shell commands. However completions for more esoteric commands such as Android debug bridge adb and cloud hosting remote client heroku are being collected on Github, also available in AUR for Arch Linux users:


yaourt -S zsh-completions

My .zshrc

So far my .zshrc looks like this:


ZSH=/usr/share/oh-my-zsh/
ZSH_CUSTOM=~/.oh-my-zsh/custom/
ZSH_THEME="robbyrussell"
DISABLE_AUTO_UPDATE="true"

plugins=(git archlinux django sublime themes python pip node npm history-substring-search zsh-syntax-highlighting)

source $ZSH/oh-my-zsh.sh

eval "$(fasd --init auto)"
alias zshconfig="subl ~/.zshrc"

Any other shell superpowers I've missed? Let me know!