Making your command line effective and enjoyable — Part 1: oh-my-zsh
zshclioh-my-zshIntroduction #
Why should I read this? #
Do you find yourself occasionally using the command line during your work, but spend most of that time thinking "I wish I could be writing code"? Do you...
The goal of this article is to use a simple but powerful tool to make any time you spend on the command line:
- Less frustrating
- More intuitive
- More efficient
Without requiring any advanced knowledge of the command line or shell scripting. By using free tools, you get access to a lot of powerful functionality for very little effort.
Background #
What is Zsh? #
Zsh is a shell. You are probably familiar with Bash which is the default shell on most Linux distributions. On it's own Zsh does not provide huge advantages over Bash, but when combined with the community maintained oh-my-zsh it will significantly improve your command line experience. I have found it not only makes me more efficient, but it also makes my time spent on the command line far more enjoyable.
What is oh-my-zsh? #
Oh My Zsh is an open source, community-driven framework for managing your zsh configuration.
source: oh-my-zsh readme
Essentially, it gives you a powerful Zsh configuration with very little work on your end.
Getting it set up #
Install Zsh and oh-my-zsh #
Install zsh, git, and curl #
on Ubuntu #
sudo apt-get install zsh git curl
on MacOS: #
brew install zsh git curl
If you don't have brew
installed, see (how to install homebrew)
Change default shell to zsh #
sudo chsh USERNAME -s /usr/bin/zsh
Install oh-my-zsh #
(source)
git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
Pick your plugins #
A large part of the power of oh-my-zsh comes from plugins. Each plugin adds useful functionality to your shell for a particular program, language, or framework. There are plugins for git, python, heroku, docker, etc.
My recommendation is to start with only a few at first and then add more as you see fit. For me, the only plugin I always use is the git plugin.
You can see the list of available plugins in ~/.oh-my-zsh/plugins
To select your plugins: #
-
open your
~/.zshrc
using your text editor of choiceexamples
with nano
nano ~/.zshrc
with vscode:
code ~/.zshrc
-
find the line that looks like
plugins=()
-
you chose which plugins are loaded by placing them inside the
()
separated by spacesexample - git:
plugins=(git)
example - git and python:
plugins=(git python)
-
after changing
~/.zshrc
, you need to reload any open shells load the plugins by running:source ~/.zshrc
Disabling a plugin #
If you ever want to disable a plugin, just remove it from the list and reload with
source ~/.zshrc
Pick your theme #
Your oh-my-zsh theme determines the look & feel of your terminal. You could make your own theme if you wanted, but there are so many provided by the community that you will probably find one you like.
First, go here and find a theme you like. Don't overthink it - you can always change it very easily at any time. I prefer aussiegeek
, but it is just personal preference so chose what fits you.
Once you have selected your theme, you have to activate it:
-
To set your theme, open
~/.zshrc
in your text editor -
near the top is a line like this
ZSH_THEME="robbyrussell"
replace
robbyrussell
with the name of the theme you wantZSH_THEME="aussiegeek"
-
save your changes to
~/.zshrc
and reload it withsource ~/.zshrc
If you have problems with fonts in your theme, you might need to install powerline fonts. On Ubuntu, this is just sudo apt-get install fonts-powerline
Exploring the awesome #
Prefix based command history #
In Bash, you can cycle through your past commands with the up arrow. This is useful, but only if the command you need was one you used quite recently. This Zsh setup makes this feature much more powerful by providing command history that is filtered by prefix. For example, if you type sudo
and then press the up arrow, you will cycle through all commands in your history which start with sudo
.
In my opinion, this is the single most useful feature you get from using oh-my-zsh. It has saved me an incredible amount of time. Now that I have it, I can't imagine using the command line without it.
Useful general purpose aliases #
oh-my-zsh provides a variety of useful aliases for common commands. These will save you a lot of typing. All of these can be changed, but I find the defaults to be quite nice. Some examples:
ll
instead ofls -l
l
instead ofls -la
To see all the aliases available in your shell, run the alias
command.
Plugins provide aliases to shorten common commands #
Plugins also provide useful aliases for commonly used commands in popular tools. For example, the git plugin provides aliases such as:
gst
--->git status
gd
--->git diff
Once you've activated a plugin you have all its aliases available to you.
Most plugins will provide a set of aliases to shorten common commands.
How do you find what they are? The alias
command lists all available aliases, but this will be a very long list. You can read over that whole list if you'd like, but my recommendation is to use alias | grep 'git'
to see all aliases containing git
.
My advice is whenever you find yourself running any given command often (e.g., git commit
) check to see if there is an alias for it and switch to using that.
While it is difficult to memorize a long list of aliases right away, I have found success in trying to learn 1-2 new aliases at a time. They will quickly become a natural part of your workflow. Within a few weeks the majority of your commands will be written using aliases and you'll be typing a lot less on the command line.
Conclusion #
What was it all for? What did I gain from reading this? #
This guide touches on a few of the most powerful features of oh-my-zsh for common development purposes. This is only the tip of the iceberg. The community behind oh-my-zsh has created a vast array of useful plugins and themes. No matter what language or framework you use, there are plugins for many of them from the common to the mundane. The litany of themes available means you will almost certainly find one that fits you.
By completing this guide, you have provided yourself with a significant power-up in your developer tool set for minimal time and effort. By using free tools built by the developer community, you have enhanced your command line with a handful of very powerful features that will make any amount of time you spend on the command line more enjoyable and efficient. The more time you spend on the command line, the more you will be grateful for these tools.
I hope you will find your time spent on the command line more effective and enjoyable now.
Appendix #
Resources #
- Most of the crucial info here can be found in the oh-my-zsh readme. I would recommend giving that a read as well, especially if you like to get into the details
- To learn more about aliases check this out
A more detailed look at aliases #
Aliases are quite helpful. They are simply a shorthand for another command. They take the following form:
alias slc="some_long_command"
If you had the above alias in your ~/.zshrc
file, anytime you ran slc
, it would be equivalent to running some_long_command
.
For example: alias gd="git diff"
- running gd
will simply run git diff
Aliases also work with options: gcb='git checkout -b'
After you've internalized the most useful aliases provided by oh-my-zsh plugins, you may find yourself longing for new custom aliases to fit your needs. Fortunately it is extremely easy to make any alias you want. Simply follow the format above and place each alias on a separate line in your ~/.zshrc
(don't forget to reload with source ~/.zshrc
). Here are some examples from my ~/.zshrc
:
alias shrug="echo '¯\_(ツ)_/¯'"
alias agrep="alias | grep"
alias lg="ll | grep"
alias lag="la | grep"
alias nrc="nano ~/.zshrc"
alias src="source ~/.zshrc"
alias genpwd="gpg --gen-random --armor 1 128"
alias vup="vagrant up"
alias vssh="vagrant ssh"
alias vhalt="vagrant halt"
alias vr="vagrant reload"
alias vrp="vagrant reload --provision"
Tip - avoid mistakes by using different themes in different environments #
Have you ever had that moment where you thought your were running a command on your laptop, but after pressing ENTER
you realized you were actually logged into your production server? If you haven't you're one of the lucky ones. Either way, there is a simple way to use oh-my-zsh themes to help prevent you from making this potentially costly mistake.
The idea is to use a different theme (optimally one that looks very different) on your servers from the one you use on your development machine. This means your local terminals will look distinct from your servers, and thus you will be more likely to notice with you are using.
There are other solutions to this as well, such as modifiying the status line to say "PRODUCTION" or something similar.