Oh my zsh
I spend a lot of my day in the terminal, and my shell of choice is Zsh — a highly customizable Unix shell that packs some very powerful features. I’m always looking for ways to type less and to automate all the things. Luckily this is something that Zsh lends itself well to.
In this post, you will find commands, plugins, aliases and tools that will save you some keystrokes and make you more productive in your day-to-day work.
If you don’t have Zsh installed on your machine, then check out this post to get it up and running.
Things Zsh Can Do out of the Box
Zsh shares a lot of handy features with Bash. None of the following are unique to Zsh, but they’re good to know nonetheless. I encourage you to start using the command line to perform operations such as those listed below. It might seem like more work than using a GUI at first, but once you get the hang of things, you’ll never look back.
- Entering
cd
from anywhere on the file system will bring you straight back to your home directory. - Entering
!!
will bring up the last command. This is handy if a command fails because it needs admin rights. In this case you can typesudo !!
. - You can use
&&
to chain multiple commands. For example,mkdir project && cd project && npm init -y
. - Conditional execution is possible using
||
. For example,git commit -m "whatever..." || echo "Commit failed"
. - Using a
-p
switch with themkdir
command will allow you to create parent directories as needed. Using brace expansion reduces repetition. For example,mkdir -p articles/jim/sitepoint/article{1,2,3}
. - Set environment variables on a per-command basis like so:
NODE_DEBUG=myapp node index.js
. Or, on a per-session basis like so:export NODE_DEBUG=myapp
. You can check it was set by typingecho $<variable-name>
. - Pipe the output of one command into a second command. For example,
cat /var/log/kern.log | less
to make a long log readable, orhistory | grep ssh
to search for any history entries containing “ssh”. - You can open files in your editor from the terminal. For example,
nano ~/.zshrc
(nano),subl ~/.zshrc
(Sublime Text),code ~/.zshrc
(VS Code). If the file doesn’t exist, it will be created when you press Save in the editor. - Navigation is an important skill to master. Don’t just rely on your arrow keys. For example,
Ctrl + a
will take you to the beginning of a line. - Whereas
Ctrl + e
will take you to the end. - You can use
Ctrl + w
to delete one word (backwards). Ctrl + u
will remove everything from the cursor to the beginning of the line.Ctrl + k
will clear everything from the cursor to the end of the line. These last three can be undone withCtrl + y
.- You can copy text with
Ctrl + Shift + c
. This is much more elegant than right clicking and selecting Copy. - Conversely, you can paste copied text with
Ctrl + shift + v
.
Try to commit those key combos to memory. You’ll be surprised at how often they come in handy.
Cool Things You Can Do with (Oh My) Zsh
Oh My Zsh is a community-driven framework for managing your Zsh configuration and comes bundled with thousands of helpful functions, helpers, plugins and themes. If you’re going to make the Z shell your daily driver, you should really install Oh My Zsh.
Here are useful things Oh My Zsh can do for you:
- The
take
command will create a new directory and change into it. take my-project replaces mkdir my-project && cd my-project. zsh_stats
will give you a list of the top 20 commands and how many times they’ve been run.- Oh My Zsh simplifies navigating your file system. For example,
..
is an alias forcd ..
. - In the same way,
...
moves you up two directories,....
moves you up three, and.....
moves you up four. - You can omit the cd when navigating. Typing
/
, for example, will take you straight to your filesystem root. - Partial matching is also supported. For example, typing
/u/c/De
and pressingTAB
, thenReturn
, takes me to/Users/conrad/Desktop/
on my Mac, on Linux it should be eg./h/c/De
. rd
is an alias forrmdir
andmd
is an alias formkdir -p
.- You can type
d
to list the last used directories from a terminal session. - You can then navigate to any of these using
cd -n
, wheren
is the directory number. - Tab completion is another great feature. For example, typing
ls -
and pressingTAB
will list all of the command’s options, along with a helpful description of what they do. This also works forcap
,rake
,ssh
, andkill
. - Typing
alias
lists all of your current aliases. - With globbing (a Zsh feature), you can list files with a particular extension. For example,
ls *.html
will list all HTML files in the current directory. To include subdirectories, change to:ls **/*.html
. - Glob qualifiers allow you to select types of files by using flags. For example,
ls -l **/*(.x)
will find all executable files in the current directory and all sub-directories. - You can search for files by date modified. For example,
ls *(m-7)
will list all files modified within the last week. - You can search for files by size. For example,
ls *(Lm+1)
will find all files with a size larger than 1MB.
Using Plugins for Fun and Profit
Oh My Zsh ships with a lot of plugins. You should look through these and invest some time learning those that will help your workflow.
Here is a plugins I regularly use, that provide a ton of handy shortcuts and aliases.
Git
The git plugin provides many aliases and several useful functions. Why not go through these and attempt to memorize your top ten? Here are the ones I use most often.
g
is a handy alias forgit
. This means you can type things likeg clone <whatever>
instead ofgit clone <whatever>
. Might only be two keystrokes, but they soon add up.gaa
is an alias forgit add all
. I use this one all the time.gb
is an alias forgit branch
, which will list all of the branches in the current repo and show you which one you’re on.gcb
is an alias forgit checkout -b
, the command that allows you to create a new branch.gcm
is an alias forgit checkout main
. This returns you to the main branch.gdca
is an alias forgit diff --cached
. This allows you to diff any files you’ve staged for commit.gf
is an alias forgit fetch
.gm
is an alias forgit merge
.gp
is an alias forgit push
. To sync a fork of a repo, you could do:gf upstream
,gm upstream/main
, followed bygp
.glog
is an alias forgit log --oneline --decorate --graph
, which will give you a pretty git branch graph.
Additional Resources
The main job of the plugins listed above is to provide aliases to often-used commands. Please be aware that there are lots more plugins out there that augment your shell with additional functionality.
Here are four of my favorites:
- sudo allows you to easily prefix your current or previous commands with
sudo
by pressingESC
twice. - autosuggestions suggests commands as you type based on history and completions. If the suggestion is the one you’re looking for, press the → key to accept it. A real time saver!
- z is a handy plugin that builds a list of your most frequent and recent folders (it calls these “frecent”) and allows you to jump to them with one command.
And don’t forget, if you spend a lot of time in the terminal, it’s worth investing some effort in making it visually appealing. Luckily, Oh My Zsh ships with a whole bunch of themes for you to choose from.