Oh-My-Zsh! A Work of CLI Magic — Tutorial for Ubuntu
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
, andzlogout
- 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.
- Install prerequisite packages
$ sudo apt install git-core zsh - 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)" - Install the Powerline font to spice up your CLI with icons
$ sudo apt install fonts-powerline - Change theme from ‘robbyrussell’ to ‘agnoster’ for the legendary Oh-My-Zsh theme
$ nano ~/.zshrc - Find the
ZSH_THEME
variable and change it:ZSH_THEME="agnoster"
- 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 - Next we open the theme file for ‘agnoster’ in the editor
$ nano agnoster.zsh-theme - 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 thePROMPT
variable to$(build_prompt)
.
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 .zshrc
file:
$ source ~/.zshrc
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 togrep
to search for a particular alias./
:cd /
~
:cd ~
...
:cd ../..
....
:cd ../../..
.....
:cd ../../../..
I think you get it, right?take test_folder
: Creates a folder namedtest_folder
and changes to itx
: Extract archive of typestar, 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.
- Oh-My-Zsh: http://ohmyz.sh/
- Plugins: https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins
- Themes: https://github.com/robbyrussell/oh-my-zsh/tree/master/themes/
- Git plugin with all aliases: https://github.com/robbyrussell/oh-my-zsh/wiki/Plugin:git
- Customizing your shell prompt: http://www.nparikh.org/unix/prompt.php#zsh
- ArchWiki about Zsh: https://wiki.archlinux.org/index.php/zsh#Autostart_X_at_login
- Zsh syntax highlighting plugin: https://github.com/zsh-users/zsh-syntax-highlighting
- Zsh autosuggestions plugin: https://github.com/zsh-users/zsh-autosuggestions
- RobbyRussell Wiki about customization of Zsh: https://github.com/robbyrussell/oh-my-zsh/wiki/Customization