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

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

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.
Oh-My-Zsh! A Work of CLI Magic — Tutorial for Ubuntu (4)
  • 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
Oh-My-Zsh! A Work of CLI Magic — Tutorial for Ubuntu (5)

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).
Oh-My-Zsh! A Work of CLI Magic — Tutorial for Ubuntu (6)

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.

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.

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

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

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

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.

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

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.

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

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.

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.

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

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

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

Oh-My-Zsh is just awesome!

This article was made with the help of TheLedger.

Oh-My-Zsh! A Work of CLI Magic — Tutorial for Ubuntu (2024)
Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 6309

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.