Unix ZSHell Reloaded

In the good old days, when there were no such things like “Graphical User Interfaces”, a computer could only be used with a command-line interface. This system was thoroughly refined to perfection and still can be used really efficiently even today. One of the major shells was the Bourne-Shell that uses the short abbreviation sh. It was used widely and at one point received an update called Bourne-Again Shell - bash. Bash is the standard on many operating systems today. Although it is so popular there have been some other contestants which have even more features. One of them is zsh the Z-Shell.

Problem:

It is not always simple to tinker around with these shells, set them up correctly, and customize to your very own needs.

Oh-My-ZSH

oh-my-zsh which was written by Robby Russell tries to solve this problem and in my opinion, succeeds in doing so. It is easy to install, use and customize and makes everyday working with zsh a charm. Check out the official website.

Installation

Installation is as simple as a one-liner:

curl -L http://install.ohmyz.sh | sh

Hopefully you will get to see this awesome piece of ASCII art at one point:

At one point this script will ask you for your admin password: It needs it to change your default shell to zsh. If you have a Homebrew version of zsh installed this will not succeed since the chsh (change shell) command only allows system shells. (At least it did so for me.) If you run into the same problem just quickly type this into your current shell.

chsh /bin/zsh

And voilà! The next time you start a terminal or open a new tab you are good to go.

Customization

In your home-folder you will now find your brand-new .zshrc configuration file.

# Path to your oh-my-zsh configuration.
ZSH=$HOME/.oh-my-zsh

# Set name of the theme to load.
# Look in ~/.oh-my-zsh/themes/
# Optionally, if you set this to "random", it'll load a random theme each
# time that oh-my-zsh is loaded.
ZSH_THEME="robbyrussell"

# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

# Set to this to use case-sensitive completion
# CASE_SENSITIVE="true"

# Comment this out to disable weekly auto-update checks
# DISABLE_AUTO_UPDATE="true"

# Uncomment following line if you want to disable colors in ls
# DISABLE_LS_COLORS="true"

# Uncomment following line if you want to disable autosetting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment following line if you want red dots to be displayed while waiting for completion
# COMPLETION_WAITING_DOTS="true"

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
plugins=(git)

source $ZSH/oh-my-zsh.sh

# Customize to your needs...

This file is pretty self explanatory. Two of the most important customization options are themes and plugins. The creator’s theme is the default when first installed and only one plugin is active.

For plugins I suggest you walk through the plugin directory or the plugin wiki and check out what is available and just add whatever you think is needed to your configuration file.

Extending Oh My ZSH

Go to your ~/.oh-my-zsh directory. Create a new directory inside of the custom directory called plugins. Here we can create any custom plugins we want. Create a new folder called test and inside of it touch a new file called test.plugin.zsh.

c() { cd ~/some/important/dir/$1; }
_c() { _files -W ~/some/important/dir -/; }
compdef _c c

This will add the c command that can quickly cd into an important directory. The second line even makes auto completion possible, it will list all the files and directories that are so important to you.

Theming your ZSHELL

For now lets look at Robby Russell’s default theme.

PROMPT='%{$fg_bold[red]%}-> %{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}'

ZSH_THEME_GIT_PROMPT_PREFIX="git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}x%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

It is pretty basic:

Looks pretty complicated? It is not that bad once you get a grasp of it.

%{$fg_bold[red]%} PLACE HOLDER

This would prompt PLACE HOLDER in red.

%{$fg[cyan]%} %c

With the %c option you can display the current directory. Loot at some of the Prompt Options that are available.

ZSH_THEME_GIT_PROMPT_PREFIX="git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}x%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

These variables are for the git_prompt_info customization. They work exactly like the rest of the Prompt.

Last but not least you can reset everything.

%{$reset_color%}

Make sure to check out my post about ANSI Escape Codes if you want to learn more about coloring and formatting your terminal!

And now?

In the end you can customize your shell however you want. If you need some inspiration and help look at both the themes wiki and the themes directory. You can get some ideas of what you want to do and of how to add some other information. Look at some of the Prompt Options to add more system info.

Usage

A quick list of features:

  • Auto completion everywhere thanks to the many plugins:
    • Just press tab!
  • Double Asterix for deeply nested search
    • ls ~/**/*.py all python files in your home directory
  • Filtered History for quicker history searching
    • $ l pressing the up button now will only result in elements starting with l
  • Multiple directory auto complete
    • This cd /on{tab}/tw{tab}/thr{tab} is just this /on/tw/thr{tab}
  • Renaming working directory
    • cd boring fun replaces all occurences of boring with fun in the path to the current directory and switches your session into the new path

End Note

This is a call to every shell user:

Change now! Because ZSH will make your life a lot easier.

By Cecil Wöbker


I do science during the day and develop or design at night. If you like my work, hire me.

Feel free to follow me on Twitter or email me with any questions.

Comments