Oh-My-Zsh! A Work of CLI Magic — Tutorial for Ubuntu

Michiel Mulders
wearetheledger
Published in
6 min readAug 29, 2017
Oh-My-Zsh Agnoster Theme

I’m a big fan of the command line interface… I hate using my computer mouse! This motivated me to search for great tools to enhance my user experience on the CLI. I came across Oh-My-Zsh during my internship. Some friends convinced me to use this shell because of its simplicity and adjustability. As a bonus, the CLI is extendable with many plugins.

This is a list of its capabilities:

  • Command validation
  • Spelling correction
  • Sharing of command history among all running shells
  • Themeable prompts (Agnoster, RobbyRussell, …)
  • Directory history
  • Startup/shutdown scripts via zshenv, zprofile, zshrc, zlogin, and zlogout
  • Strong autocomplete capabilities. You can use the TAB key to navigate through the different options and use enter to select the right folder. Bash for example would print all the options. This is fairly spammy and pollutes your scrollback.
  • Add plugins: e.g. Git plugin with a huge list of useful Git aliases.
    This plugin shows the active branch and gives visual feedback about your Git status:
    - Green: branch if no changes occurred
    - Yellow with a circle icon: untracked files
    - Yellow with a plus icon: files ready to be committed

Installation Guide

I’m performing this installation guide on my Ubuntu 16.04 Virtual Machine. To show you the underlying power of Oh-My-Zsh, I will install the Git plugin (Git-core). This plugin gives visual feedback about the Git status of a project.

  1. Install prerequisite packages
    $ sudo apt install git-core zsh
  2. Install Oh-My-Zsh from Robby Russell’s repository
    $ sh -c “$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
  3. Install the Powerline font to spice up your CLI with icons
    $ sudo apt install fonts-powerline
  4. Change theme from ‘robbyrussell’ to ‘agnoster’ for the legendary Oh-My-Zsh theme
    $ nano ~/.zshrc
  5. Find the ZSH_THEME variable and change it:ZSH_THEME="agnoster"
  6. I don’t like it that the theme shows my username and host. To get rid of this, we change the directory to
    $ cd ~/.oh-my-zsh/themes
  7. Next we open the theme file for ‘agnoster’ in the editor
    $ nano agnoster.zsh-theme
  8. Now we can change the ‘Main prompt’. We don’t need to prompt_context in the function build_prompt(). Just comment out this line or remove it. At last, change the PROMPT variable to $(build_prompt).
Agnoster theme configuration

To actually see the theme, you have to source your .zshrc file like this: source ~/.zshrc. If everything worked out fine, you should see something like the cover image!

Extra: Set zsh as our default shell. Execute this in your default shell, in most cases this will be bash. If you execute this command in zsh, it won’t change anything:
$ chsh -s $(which zsh)

Note: If you use sudo it will change the shell for root, but not for your working user. Besides that, make sure to logout and login again to see the changes.

Install Plugins

All plugins listed on the plugins Github page are pre-installed with Oh-My-Zsh at ~/.oh-my-zsh/plugins. Custom plugins can be installed at ~/.oh-my-zsh/custom/plugins. To use a plugin, you can simply add it to the plugins list in your ~/.zshrc file. Add wisely, as too many plugins slow down shell startup. Leave a blank between each plugin.

Colored-man-pages

In this example, I’ve installed a useful plugin to give color highlighting to your man pages . To be able to use the plugin, source your .zshrcfile:
$ source ~/.zshrc

Colored Man Page of ls command

Zsh-syntax-highlighting

Another great plugin is syntax highlighting for your shell. Besides that, this plugin has the capability to verify the correctness of your command.

This plugin isn’t installed with Oh-My-Zsh. Navigate to ~/.oh-my-zsh/custom/plugins and clone the code from Github into this folder:
$ git clone https://github.com/zsh-users/zsh-syntax-highlighting

You will see a folder named zsh-syntax-highlighting. This is the name that needs to be added to the plugins list.

Zsh-autosuggestions

You can also use zsh-autosuggestions for command completion. It suggests commands based on your command history. Very useful! To select the proposed command, press the right arrow key.

Installation is the same as with zsh-syntax-highlighting :
$ git clone https://github.com/zsh-users/zsh-autosuggestions
And add zsh-autosuggestions to the plugins list.

Takes Care of Alias Enthusiast

Directory History

Oh-My-Zsh automatically remembers the 20 last directories you have visited. You can list the history chronologically with dirs -v or just d.

Normally, you can use cd +1 to go to the previous directory and so on. Oh-My-Zsh made this even more straightforward, now you can use just the number of the entry in the directory history you want to visit… 1.

Other Interesting Aliases

  • alias : List all aliases. You can pipe the output to grep to search for a particular alias.
  • / : cd /
  • ~ : cd ~
  • ... : cd ../..
  • .... : cd ../../..
  • ..... : cd ../../../.. I think you get it, right?
  • take test_folder : Creates a folder named test_folder and changes to it
  • x : Extract archive of types tar, bz2, rar, gz, tbz2, tgz, zip, Z, 7z
  • upgrade_oh_my_zsh : Easy commando for upgrading your installation

Well-known Problems & Solutions

Corrupt History File

It can happen the history file gets corrupt. Don’t ask me the specific details. Each time you launch a new terminal, you get this message:

zsh: corrupt history file /home/myusername/.zsh_history

Let’s fix it with the following commands. Make sure you are in the root of homefolder. The fc command lists, edits, or re-executes commands previously entered to a shell. The history command allows you to use words from previous command lines in the command line you are typing.

mv .zsh_history .zsh_history_bad
strings .zsh_history_bad > .zsh_history
fc -R .zsh_history

Conclusion

Oh-My-Zsh is just awesome!

Useful Links and Sources

This article was made with the help of TheLedger.

--

--

wearetheledger
wearetheledger

Published in wearetheledger

An awesome collections of stories from our Blockchain experts in Belgium & The Netherlands.

Michiel Mulders
Michiel Mulders

Written by Michiel Mulders

Writing gists about marketing, lifestyle, self-help, finance, and UI/UX. Get me a Belgian beer please!

Responses (14)