A 10 minute guide to effective Terminal usage
Your command-line interface (CLI) is your virtual home, and like your physical abode, you want it to be comfortable and efficient. After all, we spend a significant portion of our day working on the terminal, so even small improvements can make a massive impact on our productivity. As developers, we’re not paid to stare at screens all day, but rather to deliver results and business value.
To help you achieve optimal performance, I’ll share my personal preferences on how I like my CLI set up. These opinions are based on years of experience, and I suggest starting with them until you have enough expertise to develop your own. Remember, your CLI is your home, so take the time to make it feel like one!
First, a note on documentation conventions
Understanding conventions in example commands can be challenging, especially when it comes to knowing what to replace. Here are two common conventions to help you:
- Firstly, if you encounter angle brackets <like this>, it is a clear indication that you need to substitute the content inside the brackets with specific information relevant to you. Do not forget to remove the angle brackets! For instance, if you come across a link like this: https://github.com/<your username> The documentation author expects you to replace <your username> with your actual username, without the angle brackets, resulting in a finished example like: https://github.com/ersakshamjain.
- Secondly, when you see a command that begins with a leading dollar sign ($), it is an indication that the author wants you to execute that command in your terminal. You should not include the dollar sign when running the command. For instance, if you see the instruction:
$ cd ~/.ssh
Just copy cd ~/.ssh and execute it in your terminal without the $. The $ symbol is merely an indication that the command should be run in your terminal.
iTerm
I have a confession to make: I’m an iTerm2 fanatic. It’s my terminal of choice, and I just can’t help but recommend it to everyone. But let me tell you, it’s not because my friend made fun of me for using Apple’s default terminal. (Okay, maybe that was part of it, but let’s not dwell on that.)
The truth is, iTerm2 has so many features that make my life easier that I don’t even remember what the default terminal looks like. And the good news is, most of the tips I’ll share with you work in both iTerm2 and the default terminal, so you can decide which one you prefer.
If you’re not already using iTerm2, you can easily download it from their website. But there’s one thing you should be aware of before you start using it: by default, iTerm2 has a limited scrollback buffer. This means that if you have a lot of terminal output, you might not be able to see it all without scrolling up. And who has time for that?
To fix this, simply follow these steps:
- Make sure iTerm2 is open and in focus (i.e. it’s the active application)
- Press command + comma (⌘,) to open iTerm2’s preferences (and practice using keyboard shortcuts, it’ll save you time!)
- Click “Profiles”
- Click “Terminal”
- Under “Scrollback Buffer,” check “Unlimited scrollback”
- Press command + Q (⌘Q) to close iTerm2 entirely (instead of clicking the little red circle)
- Press command + space bar (⌘ + space) to open Spotlight Search
- Search for iTerm and hit enter to open iTerm again (and practice always using Spotlight Search to open applications)
There you have it, folks. A small tweak that will save you time and frustration in the long run. Happy iTerm2-ing!
zsh
Zsh, short for “Z shell,” is the default shell that your shiny new MacBook Pro came equipped with if you bought it after 2019. A shell, in this context, is a program that helps you communicate with your computer by passing your commands from the terminal to the kernel, which is the heart of your machine. Without a shell, you wouldn’t be able to interact with your computer via the terminal, and you’d be stuck with a fancy paperweight.
Interestingly, the name “zsh” has an amusing backstory. It was named after a teaching assistant named Zhong Shao at Princeton in 1990, where the program was first created. So, you can now impress your friends with your knowledge of the history of the shell’s name.
To check which shell your terminal is using, you can simply run the command
echo $SHELL
The “echo” command is a built-in tool in Linux that prints strings to the screen, while “SHELL” is an environment variable that stores the name of the current shell being used. Give it a try and see for yourself if your terminal is running zsh.
oh-my-zsh
oh-my-zsh is a framework for managing your zsh configuration. This sounds boring, but I assure you it is not.
To install oh-my-zsh, find the curl command in the installation guide. It probably looks like this:
$ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
(remember that the $ means “in your terminal” — don’t copy and paste that dollar sign!)
Before you run this installation command, it’s important to understand what will happen:
- The tool will install a directory at
~/.oh-my-zsh
- The tool will replace your existing
~/.zshrc
file
Your ~/.zshrc
file is the file that runs when your shell starts. It’s a place for you to configure your shell, add to your PATH , and create aliases, among other things.
(As an aside, rc
in ~/.zshrc
is a tribute to much older software; it stands for “runcom” or “runcon” short for “run command” or “run control” — a reference to MIT CTSS, the precursor to UNIX, developed in 1965. Now you know.)
Replacing your ~/.zshrc
file is a big deal, particularly if you have made changes to your file and would like to preserve those changes. Because this is a big deal, oh-my-zsh will actually save a copy of your old ~/.zshrc
file at ~/.zshrc.pre-oh-my-zsh
. If you need to get your old file back, that’s where it is.
Now that we know what will happen, go ahead and run the installation command.
In order for the installation to take effect, you might need to restart your shell. To restart your shell (without restarting your terminal) run source ~/.zshrc
People like oh-my-zsh because it has “themes” that can make your terminal pretty. Please do not spend a bunch of time configuring your oh-my-zsh theme, changing colors, etc (until you have a full time job offer, then do whatever you want). The goal here is not to make your terminal pretty, it’s to make it practical. I use the default “robbyrussell” theme, which shows me:
- What directory I’m in
- What branch I’m on
- Whether my git working tree is clean (a small x symbol at the end of the line)
Many people also like to have the command line print the current timestamp so that they can see exactly when they ran a particular command. Some themes include the timestamp.
To change the theme, you’ll need to open your new ~/.zshrc
file in vim. If you aren’t already familiar with vim, please take a moment with Vim Crash Course. Yes, we could technically open this file in a different text editor, but we’re software engineers and we’re already in our terminal, and we can quickly do this work in vim — the more that you force yourself to use vim, the more comfortable you’ll be using it.vim ~/.zshrc
to open the file with vim.
You’ll see that you now have a really long ~/.zshrc
file! It has tons of options that are commented out. You can read through those options and test out the features that sound interesting to you by uncommenting the lines that you want to have take effect.
Please install oh-my-zsh on both your laptop and your cloud desktop.
zsh-autosuggestions
zsh-autosuggestions is one of three command line tools I cannot live without. It offers command completion suggestions while you’re typing so that you only have to write the first piece of a command. Once you’ve written enough of the command that you can see the rest of the command you want as a suggestion, you can use the right arrow on your keyboard to complete your command.
Follow the zsh-autosuggestions install instructions for installing this tool with oh-my-zsh (summarized below).
Run this git clone command to clone the zsh-autosuggestions repo:
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
Open your ~/.zshrc
file with vim ~/.zshrc
and add to the plugins
section.
Restart your shell with source ~/.zshrc
Please install zsh-autosuggestions on both your laptop and your cloud desktop.
fzf
fuzzy finder! fzf turbo charges “reverse-i-search”
Before you install fzf, let’s talk about what “reverse-i-search” is. I know you know how to “up arrow” your way to old commands. Did you know that you can search them instead? Type ctrl + r/ cmd+r(on MAC)
into your terminal. This will open “reverse-i-search” (you can tell, because your command line will now say “reverse-i-search”). In this mode, you can start typing commands you’ve typed in the past, and you’ll see matches with past commands as you type. With a little practice and some tutorials, reverse-i-search can be a very powerful tool. reverse-i-search is honestly great just on its own, but fzf adds a little extra sugar.
vi
While you’re searching, fzf will show you many potential matching commands. It also lets you search for matching text in the middle of previous commands, and it’s pretty good at guessing correctly even when you have typos. Now when you think “I’m pretty sure I’ve run this command before” you can run ctrl + r
, start typing whatever you can remember, and see a bunch of potential command matches.
To install fzf, follow the instructions from the README (copied below):
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf~/.fzf/install
Restart your shell with source ~/.zshrc
To test it out, run ctrl + r
and start typing a previous command.
If you’re in fzf search mode and you don’t want to be, ctrl + c
to quit the command and regain control of your command line (this is always true of ctrl + c
— want a fresh command line? ctrl + c
will reset it for you).
z (jump around)
z is a tool that creates a database of the directories that you cd into, based on frequency and recency. Instead of running cd ../../../
back and forth into various directories you can run z <part of a directory name>
and z will guess what directory you mean. It’s good. Doesn’t matter where I am on my machine, z takes me exactly where I want to be.
The installation instructions for z are a little thin. This should work unless something has changed:
cd $HOME
git clone https://github.com/rupa/z.git
echo '. $HOME/z/z.sh' >> ~/.zshrc
source ~/.zshrc
Once installed, cd around a bunch into directories that you want in your database, then run z <some directory>
— as long as that directory name is reasonably unique, z should take you straight to that directory. Learning what directory shorthand works takes a little getting used to, but you’ll get there with practice. Learning z will save you so much time and more importantly, so much mental effort! Our brains only have so many energy to do complex things each day. Don’t waste your precious brain power manually cd’ing between directories.
That’s it!
I’ve been asking friends for their terminal tooling advice for several years. These are the only tools that have survived the transfer to new machines as I’ve replaced old laptops. I like to keep my custom tool list relatively short and simple, because the more custom tooling you add the more likely it is that something will break and cause you a headache down the road. These tools have served me well for many years.