10 Zsh Tips & Tricks: Configuration, Customization & Usage

Share this article

10 Zsh Tips & Tricks: Configuration, Customization & Usage

In this article, I’ll show you how to install zsh, then offer ten tips and tricks to make you more productive when working with it.

As web developers, the command line is becoming an ever more important part of our workflow. We use it to install packages from npm, to test API endpoints, to push commits to GitHub, and lots more besides.

My shell of choice is zsh. zsh is a highly customizable Unix shell, that packs some very powerful features such as killer tab completion, clever history, remote file expansion, and much more.

This is a beginner-level zsh guide which can be followed by anybody (even Windows users, thanks to Windows Subsystem for Linux). However, in light of Apple’s announcement that zsh is now the standard shell on macOS Catalina, Mac users might find it especially helpful.

Let’s get started.

Installing zsh

I don’t want to offer in-depth installation instructions for each operating system, but rather some general guidelines instead. If you get stuck installing zsh, there’s plenty of help available online.

At the time of writing, the current zsh version is 5.7.1.

Installing zsh on macOS

Most versions of macOS ship with zsh pre-installed. You can check if this is the case, and if so, which version you’re running using the command: zsh --version. If the version is 4.3.9 or higher, you should be good to go (we’ll need at least this version to install Oh My Zsh later on). If not, you can follow this guide to install a more recent version of zsh using Homebrew.

Once installed, you can set zsh as the default shell using chsh -s $(which zsh). After issuing this command, you’ll need to log out, then log back in again for the changes to take effect.

If at any point you decide you don’t like zsh, you can revert to Bash using chsh -s $(which bash).

Installing zsh on Linux

On Ubuntu-based distros, you can install zsh using sudo apt-get install zsh. Once the installation completes, you can check the version using zsh --version, then make zsh your default shell using chsh -s $(which zsh). You’ll need to log out, then log back in for the changes to take effect.

As with macOS, you can revert back to Bash using chsh -s $(which bash).

If you’re running a non-Ubuntu-based distro, then check out the instructions for other distros.

Installing zsh on Windows

Unfortunately, this is where things start to get a little complicated. Zsh is a Unix shell, and for it to work on Windows, you’ll need to activate Windows Subsystem for Linux (WSL), an environment in Windows 10 and 11 for running Linux binaries.

There are various tutorials online explaining how to get up and running with zsh in Windows 10. I found these two to be up-to-date and easy to follow:

Note that it’s also possible to get zsh running with Cygwin. Here are instructions for doing that.

First Run

When you first open zsh, you’ll be greeted by the following menu.

Zsh first run menu

If you select (1), you’ll be taken to a menu that allows you to configure history, keybindings and a bunch of other things. However, I suggest selecting (2), which will create a configuration profile with the recommended default settings.

Locating the Configuration File

Now let’s have a look at the file that zsh just created. Enter your home directory and open the .zshrc file in your editor of choice. This file is run whenever you start zsh and is the place where any custom configuration lives.

Note: file names that begin with a dot are hidden by default on most operating systems, so you’ll need to make it visible before you can edit it. If you’re not sure how, search for “Show hidden dot files mac/Linux etc”.

We can demonstrate how this file works by creating an alias. In their simplest form, aliases are terminal shortcuts for regular commands. Add the following to the bottom of .zshrc and save the file:

alias myip="curl http://ipecho.net/plain; echo"

Restart your shell (for example, by closing it then opening it again), then type myip. Providing you have the cURL program installed, you should see your current public IP address output to the terminal. If you’d like to experiment more with aliases, check out 7 Super Useful Aliases to make your development life easier.

Shut the terminal by typing exit, then locate the .zsh_history file, which is also in your home directory. This is where zsh stores a list of your previous terminal commands. Currently it should contain two lines — myip and exit.

Note: we’ll be reloading our shell several times through out the tutorial. You can also do this with a command: source ~/.zshrc.

Check Your Progress

By this point you should have zsh installed and set as your default shell. If you experience any problems, try creating a topic on SitePoint’s forums and ask for help there.

Now let’s get into some tips.

1. Install Oh My Zsh

If you only follow one of these tips, it needs to be this one.

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. Many of the following tips will rely on you having this installed.

As is explained on the project’s home page, you can install it using cURL or wget:

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

This assumes you have Git installed, which you hopefully do already. If not, you can grab it from the project’s homepage.

Oh My Zsh post install message

As you can see, from the post installation message, Oh My Zsh created a backup of your .zshrc file, then replaced it with its own version. This means that you’ll need to copy over any custom configuration (such as our myip alias) to your new .zshrc file.

2. Select a Theme

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.

If you look in the ~/.zshrc file for the ZSH_THEME variable, you’ll find it’s set to robbyrussel. This is the default theme that the creator of Oh My Zsh uses.

To install a different theme, pick one from the link above and change the value of the ZSH_THEME variable, before restarting your shell. For example, setting ZSH_THEME="avit" will cause Oh My Zsh to use the avit theme.

You might also like to alter the color scheme for your terminal. On my Linux machine, I could do this via Edit > Preferences > Colors. Removing the tick from Use colors from system theme, then selecting Solarized dark as a scheme, gives me a very appealing result in next to no time.

Avit theme on Oh My Zsh

Notice how the theme recognizes that it’s in a Git repo, tells me which branch I’m on, and displays a green tick to let me know that everything is up to date.

I encourage you to have a look through the available themes and find one that suits you.

Troubleshooting

When I was testing this on WSL, I was seeing broken characters in my terminal, as soon as I installed Oh My Zsh. To remedy this, I had to install the Powerline fonts and tell my terminal to use them:

git clone https://github.com/powerline/fonts.git

Then open an admin PowerShell, navigate to the root of the repo and run:

.\install.ps1

If PowerShell blocks you from running the script, then try setting the ExecutionPolicy as per this Stack Overflow answer. Once the fonts are installed, be sure to set it back again.

Finally, right click on the terminal’s title bar, choose Properties > Font and make sure a Powerline font is selected. In testing, Deja Vu Sans Mono for Powerline worked well for me.

Also note that a couple of the themes require Powerline fonts, regardless of the operating system. We’ll look at installing one of these themes (Agnoster) towards the end of the article.

3. Working with History

One of my favorite Oh My Zsh features is the way that it lets you search through your history. Imagine that the contents of .zsh_history look like this:

cd Desktop
mkdir my-project
cd my-project
npm init -y
mkdir {src,build}
touch index.js

If you’re in the terminal and press the key, you’d expect to cycle through each of those history entries one by one, right? No surprise there.

However, Oh My Zsh allows you to enter the beginning of a command, then press the key to cycle through matching entries. So, in the above example if you entered “mkdir” and then pressed the key, you would see “mkdir {src,build}” displayed. If you pressed it again, you would then see “mkdir my-project”.

I use this feature all the time. For instance, when I want to ssh into a server and can’t remember the exact credentials, I just enter “ssh” and can cycle through recent connections until I find the right one. Or, when I can’t remember the exact syntax of a command, I can just enter the first few characters and cycle through any matches.

4. Autosuggestions FTW!

Another great feature of Oh My Zsh is plugins. These add new features to your shell and augment its functionality.

Oh My Zsh ships with hundreds of plugins for every conceivable use and we’ll look at some of these later in the article. In this section however, I’d like to introduce you to a third party plugin called autosuggestions. This suggests commands as you type based on history.

To install, you need to clone its repository into $ZSH_CUSTOM/plugins. By default this is ~/.oh-my-zsh/custom/plugins:

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

Once done, to activate it, hop into the ~/.zshrc file and uncomment the following line:

# plugins=(git)

Then replace git with zsh-autosuggestions:

plugins=(zsh-autosuggestions)

Restart your shell, and you’ll notice that as you type a command, Oh My Zsh will make suggestions based on your history. If the suggestion is the one you are looking for, press the key to accept it. A real time saver!

Autosuggestions with Oh My Zsh

5. File Navigation with Oh My Zsh

Another awesome thing about Oh My Zsh is how easy it makes it to navigate around your machine.

For example, when you want to change directory, it isn’t necessary to enter the cd command; cd Desktop just becomes Desktop. And that works with paths, too: you can enter /home/jim/Desktop and end up right where you wanted to go.

Oh My Zsh has great autocomplete, too. Once you have started typing a path, you can press TAB to have it expand a list of all possible folders you can navigate to. If you continue tabbing, you can move through this list until you select a folder by pressing Return. Pressing TAB again after that will restart the process.

Oh My Zsh file navigation with TAB completion

Finally, it’s worth mentioning that Oh My Zsh provides a number of commands and aliases for working with, and navigating directories. For example, typing ... will move you up two directories and typing take <dir-name> will create a new directory and navigate to it. Oh My Zsh also supports dynamic path completion, so typing (for example) /h/j/De and pressing Tab will expand the path to /home/jim/Desktop. Cool, eh?

Bonus Tip: if you’re feeling lazy, you can just drag and drop a folder onto the terminal window and the path will be expanded for you.

6. Navigate to Anything Using z

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. This is often much more convenient than having to tab through a nested folder structure.

To install the plugin, all you have to do is add it to your .zshrc file like so:

plugins=(z zsh-autosuggestions)

Then restart your terminal.

Once installed, z will have a short learning phase as it observes you navigating around your PC with the terminal. After a while, however, you’ll be able to type z followed by any word that is in your desired directory path. The plugin will use fuzzy matching to figure out which folder you want to go to and expand the path accordingly. If there’s more than one possibility, you can tab through the options as described in the previous tip.

This might not sound like a big deal, but you’ll be able to open a shell, type z my-project and have it expand the path to /home/jim/files/some/deeply/nested/directory/or/other/my-project. Then when you’re in that directory, you can type z my-other-project and have it expand the path to /var/www/html/projects/top/secret/my-other.project.

Note: the first time I ran z, and attempted to change directory, I got the error
_z_dirs:2: no such file or directory: /home/jim/.z. This was a one off, as z apparently needed to make a .z file in my home directory. If you run into any problems, this article has some suggestions.

7. Working with Git

Zsh includes some basic Git integration out of the box. For example, if you type git and press the Tab key, zsh will helpfully spit out a long list of possible git commands. This becomes slightly more useful if you type git, followed by the first couple of letters of the command you’re looking for. For example git a + Tab produces this:

add        -- add file contents to index
am         -- apply patches from a mailbox
apply      -- apply patch to files and/or to index
archimport -- import an Arch repository into git
archive    -- create archive of files from named tree

Things start to get more interesting when you install the Oh My Zsh git plugin:

plugins=(git z zsh-autosuggestions)

This provides a whole bunch of aliases, as well as some useful functions for working with Git. Some of the ones I use frequently are:

  • gaagit add all
  • gdcagit diff --cached
  • gpgit push
  • gpf!git push --force
  • grhhgit reset --hard
  • gstgit status

You could also create an alias for several commands at once, by adding the following to your .zshrc file:

function acp() {
  git add .
  git commit -m "$1"
  git push
}

Now, when you’re in a Git repo, typing acp "a commit message" will add all changed files, commit them with whatever commit message you specified, then push them to your remote. Credit for this idea goes to Ali Spittel.

8. Get Instant Feedback on the State of a git Repo

At the top of the article, we activated a theme called avit. When in a Git repo, this theme will display the branch name, as well as a cross or tick indicating whether everything branch is up-to-date. I love being able to see this information at a glance, and it’s definitely a big step up from having to type git status all the time.

There are a couple of themes, however, that display this information in a more intuitive way. My favorite is Agnoster. Let’s install that now.

For Agnoster to render arrows and Git icons correctly, you’ll likely need to install a Powerline-patched font. WSL users can do this by following the instructions in the troubleshooting section above. For macOS and Linux users, installation instructions can be found in the project’s repo.

Mac users on High Sierra or later may need to perform some additional work to get these fonts to display. Unless you are on Catalina, this method is recommended. Catalina users will need to install a patched font and select it in their terminal settings.

To test if your terminal can display all of the necessary characters, run the following command:

echo "\ue0b0 \u00b1 \ue0a0 \u27a6 \u2718 \u26a1 \u2699"

The result should look like the image below.

Powerline fonts required for the Agnoster theme

After that, enable the theme by changing the following line in your .zshrc file:

ZSH_THEME="agnoster"

Then restart the terminal. Once Agnoster is up and running, you’ll have a more intuitive view of the status of any Git repository. You’ll also receive visual feedback as to whether the previous command succeeded or failed, and whether you are working with elevated privileges.

Up and running with the Agnoster theme

Tip: if you don’t like the user@host part of the prompt, you can make this disappear by adding DEFAULT_USER=your_user_name to the .zshrc file.

9. Plugins, Plugins, Plugins

As mentioned, Oh My Zsh ships with a lot of plugins. You really should look through these and invest some time learning those that will help your workflow. To get you started, here are some popular ones to be aware of:

  • cloudapp – uploads files and piped contents to the Cloudapp service. Outputs and copies the resultant URL to the clipboard.
  • command-not-found: if a command isn’t recognized in the $PATH, this will use Ubuntu’s command-not-found package to find it or suggest spelling mistakes (only for Ubuntu and openSUSE).
  • node: open the Node API, for your current version, in your browser.
  • npm: adds autocompletion to npm.
  • sudo: hitting ESC twice puts sudo in front of the current command, or the last one if the command line is empty.
  • vscode: makes interaction between the command line and the code editor easier.

10. Key Combos

This isn’t a zsh specific tip, but to wrap up I wanted to make you aware of these five time-saving key combos.

  • Ctrl + U: delete from the cursor to the start of the line.
  • Ctrl + K: delete from the cursor to the end of the line.
  • Ctrl + W: delete from the cursor to the start of the preceding word.
  • Alt + D: delete from the cursor to the end of the next word.
  • Ctrl + L: clear the terminal.

If you can memorize those, you’ll be surprised at how often they come in handy.

Conclusion

In this article, I’ve demonstrated how to install zsh (Z shell) and set it as your default shell. I’ve also showed how to get the most out of your new terminal with the Oh My Zsh framework. It’s my hope that, if you give these a try for a couple of weeks, you’ll wonder how you ever did without them.

And don’t forget: if you have any problems with any of this, you can hop over to SitePoint forums and ask for help there. I’d also be happy to hear if I missed anything out — for example, your favorite theme, plugin, alias, or productivity tip.

You can hit me up either on the forums or on Twitter.

FAQs about Zsh (Z shell)

How is Zsh different from other shells like Bash?

Zsh offers features like advanced tab completion, spelling correction, customizable prompts, and more scripting options. It’s designed to enhance user experience and productivity compared to traditional shells like Bash.

Can I use Zsh on Windows?

Yes, you can use Zsh on Windows by installing it through tools like Cygwin, Windows Subsystem for Linux (WSL), or Git Bash. However, setting up Zsh on Windows might involve some additional steps

Can Zsh run my existing Bash scripts?

Yes, Zsh can run Bash scripts, but some compatibility issues might arise due to syntax differences between the two shells. It’s a good practice to validate and adjust scripts to ensure they work correctly in Zsh.

What are Zsh themes?

Zsh themes determine the appearance of your terminal prompt. They often display useful information like the current directory, Git status, and more. Oh My Zsh includes a variety of themes you can choose from.

How do I enable syntax highlighting in Zsh?

You can enable syntax highlighting by using Zsh plugins like “zsh-syntax-highlighting.” Install the plugin and add its activation command to your Zsh configuration file.

Can I use my Bash configurations with Zsh?

Yes, many Bash configurations can be adapted for Zsh. However, Zsh has its own syntax and features, so some modifications might be necessary to fully utilize its capabilities.

How do I customize my Zsh prompt?

You can customize your Zsh prompt by modifying the PS1 environment variable in your Zsh configuration file (usually ~/.zshrc). Alternatively, using frameworks like Oh My Zsh allows you to choose and customize themes.

What are Zsh plugins?

Zsh plugins are extensions that enhance Zsh’s capabilities. They can provide additional features like syntax highlighting, auto-suggestions, and improved completion for specific commands or languages.

How can I install Zsh plugins?

Zsh plugin managers like “Oh My Zsh,” “Zplug,” and “Antigen” make it easy to install and manage plugins. Each manager has its own setup instructions, usually involving modifying your Zsh configuration file.

What is Oh My Zsh?

Oh My Zsh is a popular framework for managing Zsh configurations and plugins. It includes a collection of pre-built themes and plugins that you can easily enable or customize.

How can I set Zsh as my default shell?

You can change your default shell to Zsh by running the command chsh -s $(which zsh). This will update your shell preference in the system’s user configuration.

How do I install Zsh?

Zsh is often pre-installed on macOS and many Linux distributions. If not, you can install it using your system’s package manager. For example, on Debian-based systems, you can run sudo apt-get install zsh.

What is Zsh (Z shell)?

Zsh, short for Z shell, is an advanced Unix shell that provides powerful customization options, improved scripting capabilities, and interactive features for command-line interfaces.

How to Configure Zsh?

Configuring Zsh involves customizing various aspects of the shell to suit your preferences and needs. Here’s a step-by-step guide on how to configure Zsh:

Edit the Zsh Configuration File (.zshrc): The primary configuration file for Zsh is located in your home directory and is named .zshrc (i.e., ~/.zshrc). You can edit this file using a text editor of your choice, such as nano, vim, or gedit.
Choose a Zsh Framework or Plugin Manager (Optional): Zsh frameworks and plugin managers like Oh My Zsh, Prezto, and Zgen can simplify the configuration process by providing pre-configured setups and easy management of plugins and themes. You can choose one of these frameworks or manage everything manually.
Select a Zsh Theme: Zsh themes control the appearance of your terminal prompt. Many themes are available, offering different styles and information display options. To change your theme, locate the ZSH_THEME variable in your .zshrc file and set it to the name of the desired theme, such as ZSH_THEME="agnoster". Save the changes and restart your Zsh session or run source ~/.zshrc to apply the new theme.
Install and Manage Zsh Plugins (Optional): Zsh plugins can extend the functionality of your shell. If you’re using a framework like Oh My Zsh, you can easily add and manage plugins. In your .zshrc file, you’ll typically find a section for configuring plugins. You can add or remove plugins as needed, like plugins=(git zsh-syntax-highlighting zsh-autosuggestions). Save the changes and restart your Zsh session or run source ~/.zshrc to activate the plugins.
Set Zsh Options and Aliases: Customize Zsh by configuring various options and creating aliases directly in your .zshrc file. To set options, use the setopt command (e.g., setopt nocasematch). To create aliases, use the alias command (e.g., alias ll='ls -al').
Customize the Prompt (Optional): For advanced customization of your prompt’s appearance, modify the PS1 variable. This involves manually specifying the elements you want in your prompt, and there are many online resources and guides available for creating custom prompts.
Save and Apply Changes: After making your customizations, save the .zshrc file, and either restart your Zsh session or run source ~/.zshrc to apply the changes immediately.
Test and Refine: Test your customizations and make adjustments as needed. Experiment with different themes, plugins, options, and aliases to find the setup that works best for your workflow and preferences.

Remember that configuration is a personal process, and you can tailor your Zsh environment to suit your specific needs and style. Zsh’s flexibility and the availability of various themes and plugins make it a versatile shell for configuring your command-line experience.

How to customize Zsh?

Customizing Zsh allows you to tailor your shell environment to your specific needs and preferences. Here’s how you can customize Zsh:

Edit the Zsh Configuration File (.zshrc): The primary configuration file for Zsh is located in your home directory and is named .zshrc (i.e., ~/.zshrc). You can edit this file using a text editor of your choice, such as nano, vim, or gedit.
Choose a Zsh Framework or Plugin Manager (Optional): Zsh frameworks and plugin managers like Oh My Zsh, Prezto, and Zgen can simplify the customization process by providing pre-configured setups and easy management of plugins and themes. You can choose one of these frameworks or manage everything manually.
Select a Zsh Theme: Zsh themes control the appearance of your terminal prompt. Many themes are available, offering different styles and information display options. To change your theme, locate the ZSH_THEME variable in your .zshrc file and set it to the name of the desired theme. Save the changes and restart your Zsh session or run source ~/.zshrc to apply the new theme.
Install and Manage Zsh Plugins (Optional): Zsh plugins can extend the functionality of your shell. If you’re using a framework like Oh My Zsh, you can easily add and manage plugins. In your .zshrc file, you’ll typically find a section for configuring plugins. You can add or remove plugins as needed. Save the changes and restart your Zsh session or run source ~/.zshrc to activate the plugins.
Set Zsh Options and Aliases: Customize Zsh by configuring various options and creating aliases. You can do this directly in your .zshrc file. To set options, use the setopt command.
To create aliases, use the alias command.
Customize the Prompt (Optional): You can further customize the appearance of your prompt by modifying the PS1 variable. This is a more advanced customization that involves manually specifying the elements you want in your prompt. There are many online resources and guides available for creating custom prompts.
Save and Apply Changes: After making your customizations, save the .zshrc file, and either restart your Zsh session or run source ~/.zshrc to apply the changes immediately.
Test and Refine: Test your customizations and make adjustments as needed. You can experiment with different themes, plugins, options, and aliases to find the setup that works best for your workflow and preferences.

Remember that customization is a personal process, and you can tailor your Zsh environment to suit your specific needs and style. Zsh’s flexibility and the availability of various themes and plugins make it a versatile shell for customizing your command-line experience.

James HibbardJames Hibbard
View Author

Network admin, freelance web developer and editor at SitePoint.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form