Home > Technology peripherals > It Industry > 10 Zsh Tips & Tricks: Configuration, Customization & Usage

10 Zsh Tips & Tricks: Configuration, Customization & Usage

Jennifer Aniston
Release: 2025-02-09 12:32:08
Original
210 people have browsed it

10 Zsh Tips & Tricks: Configuration, Customization & Usage

This article will guide you to install zsh and provide ten tips to help you improve the efficiency of zsh usage.

As a web developer, command lines are becoming increasingly important in workflows. We use it to install npm packages, test API endpoints, push commits to GitHub, and more.

The shell I chose is zsh. zsh is a highly customizable Unix shell with powerful features such as powerful Tab key automatic completion, smart history, remote file extension, and more.

This guide is suitable for all users (even for Windows users, thanks to Windows Subsystem for Linux). Given that Apple announced that zsh is now a standard shell for macOS Catalina, Mac users may find it particularly useful.

Let's get started!

Key Points

  • Preferential Installation Oh My Zsh: Quickly start your zsh experience by installing Oh My Zsh (a community-driven framework that uses thousands of useful plug-ins and theme enhancements).
  • Theme selection is important: Choose a theme from Oh My Zsh's numerous theme libraries (such as the default "robbyrussell" or the popular "avit") to customize your terminal appearance for better visual comfort.
  • Optimize History Search: Use Oh My Zsh's history search feature to easily retrieve and execute past commands by starting to type commands and looping through suggestions using the up arrow.
  • Automatic Suggestion Plugin: Use the automatic Suggestion Plugin to increase efficiency, which makes command suggestions as you type based on your history, thus speeding up command line operations.
  • Navigation Shortcuts: Enhance directory navigation in Oh My Zsh by using shortcuts such as typing the directory name directly or jumping to commonly used and recently used directories using the "z" plugin.
  • Git integration and themes: Execute fast Git commands with Oh My Zsh's git plugin and select a theme like Agnoster for an instant and intuitive understanding of your git repository status.

Installation zsh

I don't provide detailed installation instructions for each operating system, but I provide some common guidelines. If you have problems installing zsh, there is a lot of help information online.

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

Install zsh on macOS

Most versions of macOS have zsh pre-installed. You can check if this is the case with the command zsh --version and if you have installed which version. If the version is 4.3.9 or higher, it should be available (at least this version is required for Oh My Zsh later). If not, you can follow this guide to install newer versions of zsh using Homebrew.

After the installation is complete, you can set zsh as the default shell using

. After issuing this command, you need to log out and then log in again to make the changes take effect. chsh -s $(which zsh)

If you decide at any time you don't like zsh, you can use

to restore to Bash. chsh -s $(which bash)

Install zsh on Linux

On Ubuntu-based distributions, you can install zsh using sudo apt-get install zsh. Once the installation is complete, you can check the version using zsh --version and then set zsh as the default shell using chsh -s $(which zsh). You need to log out and then log in again to make the changes take effect.

As with macOS, you can use chsh -s $(which bash) to restore to Bash.

If you are running a non-Ubuntu-based distribution, please check the instructions for other distributions.

Install zsh on Windows

Unfortunately, things started to get a little complicated. zsh is a Unix shell, and to run it on Windows, you need to activate Windows Subsystem for Linux (WSL), an environment for running Linux binaries in Windows 10 and 11.

There are many tutorials on the Internet to explain how to use zsh in Windows 10. I found the following two tutorials to be up-to-date and easy to follow:

  • How to Install and Use Linux Bash Shell on Windows 10 - First follow this step to install WSL
  • How to use Zsh (or other shell) in Windows 10 - Next, follow this step to install zsh

Please note that you can also use Cygwin to run zsh. Here are instructions for doing this.

First run

The first time you open zsh, you will see the following menu.

10 Zsh Tips & Tricks: Configuration, Customization & Usage

If you select (1), you will be taken to a menu that allows you to configure history, key bindings, and many more. However, I recommend choosing (2), which will create a configuration file with recommended default settings.

Find configuration file

Now let's take a look at the file zsh just created. Go to your home directory and open the .zshrc file in the editor of your choice. This file is run whenever you start zsh, and this is where any custom configuration is located.

Note: On most operating systems, file names starting with dots are hidden by default, so you need to make them visible to edit it. If you are not sure how to do it, search for "Show hidden dot files mac/Linux, etc."

We can demonstrate how this file works by creating an alias. In its simplest form, an alias is a terminal shortcut for regular commands. Add the following to the bottom of .zshrc and save the file:

<code>alias myip="curl http://ipecho.net/plain; echo"</code>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Restart your shell (for example, by closing it and then opening it again), and type myip. If you have installed the cURL program, you should see the current public IP address output to the terminal. If you want to try alias more, check out 7 super useful alias to make your development life easier.

Close the terminal by typing exit and find the .zsh_history file, which is also located in your home directory. This is where zsh stores your previous terminal command list. Currently it should contain two lines - myip and exit.

Note: In this tutorial, we will reload the shell multiple times. You can also do this using the command: source ~/.zshrc.

Check your progress

At this point, you should have zsh installed and set it as the default shell. If you have any problems, try creating a topic on the SitePoint forum and seek help.

Let's get into some tips now.

  1. Install Oh My Zsh

If you follow only one of these tips, this must be this.

Oh My Zsh is a community-driven framework for managing your zsh configurations, with thousands of useful functions, assistants, plugins, and themes. Many of the following tips depend on you having this framework installed.

As stated in the project homepage, you can install it using cURL or wget:

<code>alias myip="curl http://ipecho.net/plain; echo"</code>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<code>sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"</code>
Copy after login
Copy after login
Copy after login

This assumes that you have Git installed, and hopefully you have installed it. If not, you can get it from the project's homepage.

10 Zsh Tips & Tricks: Configuration, Customization & Usage

As you can see, from the post-install message, Oh My Zsh creates a backup of the .zshrc file and then replaces it with its own version. This means you need to copy any custom configuration (such as our myip alias) into a new .zshrc file.

  1. Select a topic

If you spend a lot of time on the terminal, it's worth investing some effort to make it more visually appealing. Fortunately, Oh My Zsh comes with a lot of themes.

If you look for the .zshrc variable in the ZSH_THEME file, you will find that it is set to robbyrussel. This is the default theme used by Oh My Zsh creators.

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

You may also want to change the terminal's color scheme. On my Linux machine, I can do this by editing > Preferences > Color. Uncheck Use the color in the system theme and select Solarized dark as a solution, it takes almost no time to get very attractive results.

10 Zsh Tips & Tricks: Configuration, Customization & Usage

Note how the topic recognizes how it is located in the Git repository, tells me which branch I am on, and shows a green tick to let me know that everything is up to date.

I encourage you to browse the available topics and find one that suits you.

Troubleshooting

When I test it on WSL, once I have Oh My Zsh installed, corrupted characters appear in my terminal. To fix this I had to install Powerline fonts and tell my terminal to use them:

<code>alias myip="curl http://ipecho.net/plain; echo"</code>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Then open Administrator PowerShell, navigate to the root directory of the repository and run:

<code>sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"</code>
Copy after login
Copy after login
Copy after login

If PowerShell prevents you from running the script, try setting up the ExecutionPolicy as follows this Stack Overflow answer. After the font is installed, be sure to set it back.

Finally, right-click the title bar of the terminal, select Properties > Font, and make sure that Powerline fonts are selected. In testing, the Deja Vu Sans Mono for Powerline worked well for me.

Also note that some themes require Powerline fonts, regardless of the operating system. We will explain how to install one of the topics (Agnoster) in the last part of the article.

  1. User History

One of my favorite Oh My Zsh features is the way it allows you to search history. Assume that the content of .zsh_history is as follows:

<code>sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"</code>
Copy after login
Copy after login

If you press the key in the terminal, you'd expect to loop through these history entries one by one, right? This is not surprising.

However, Oh My Zsh allows you to enter the beginning of a command and press the key to loop over the matching entries. So in the example above, if you type "mkdir" and press key, you will see "mkdir {src,build}" displayed. If you press it again, you will see "mkdir my-project".

I have been using this feature. For example, when I want to ssh to the server and don't remember the exact credentials, I just type "ssh" and can loop over the most recent connection until the correct connection is found. Or, when I can't remember the exact syntax of the command, I can just type in the first few characters and loop through any matches.

  1. Automatic suggestions

Oh My Zsh's other great feature is the plugin. These will add new features to your shell and enhance their functionality.

Oh My Zsh comes with hundreds of plugins for a variety of purposes, some of which we will cover later in this article. However, in this section, I want 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 to $ZSH_CUSTOM/plugins. By default, this is ~/.oh-my-zsh/custom/plugins:

<code>git clone https://github.com/powerline/fonts.git</code>
Copy after login
Copy after login

After finishing, to activate it, go to the .zshrc file and uncomment the following line:

<code>.\install.ps1</code>
Copy after login

Then replace git with zsh-autosuggestions:

<code>cd Desktop
mkdir my-project
cd my-project
npm init -y
mkdir {src,build}
touch index.js</code>
Copy after login

Restart the shell and you will notice that when you type the command Oh My Zsh makes suggestions based on your history. If the suggestion is the suggestion you are looking for, press to accept it. A real time-saving!

10 Zsh Tips & Tricks: Configuration, Customization & Usage

  1. Use Oh My Zsh for file navigation

Oh Another great thing about Oh My Zsh is how easy it makes navigating on your machine.

For example, when you want to change a directory, there is no need to enter the cd command; cd Desktop just enter Desktop. This also applies in the path: you can type /home/jim/Desktop and go directly to where you want to go.

Oh My Zsh also has a good automatic completion function. Once you start typing the path, you can press the TAB key to let it expand a list of all possible folders you can navigate to. If you continue to press the Tab key, you can iterate through this list until you select a folder by pressing the Return key. After that, pressing the TAB key again will restart the process.

10 Zsh Tips & Tricks: Configuration, Customization & Usage

Finally, it is worth mentioning that Oh My Zsh provides some commands and aliases for processing and navigating directories. For example, typing ... will cause you to move two directories upwards, typing take <dir-name></dir-name> will create a new directory and navigate to it. Oh My Zsh also supports dynamic path completion, so type (for example) /h/j/De and press Tab to expand the path to /home/jim/Desktop. Very cool, right?

Outline Tip: If you feel lazy, you can drag and drop the folder directly onto the terminal window and the path will expand for you.

  1. Use z to navigate to anything

z is a handy plugin that creates a list of folders you use most and recently used (it is called "frecent") and allows you to jump to them with a command. This is usually more convenient than having to do tab switching via nested folder structures.

To install the plugin, you just need to add it to the .zshrc file:

<code>alias myip="curl http://ipecho.net/plain; echo"</code>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Then restart the terminal.

After installing, z will have a short learning phase as it will observe you using the terminal to navigate around your PC. However, after a while, you will be able to type z followed by any word in the directory path you want. The plugin will use fuzzy matching to determine the folder you want to go to and expand the path accordingly. If there are multiple possibilities, you can iterate over the options as described in the previous prompt.

This may not sound like a big deal, but you will be able to open a shell, type z my-project and extend its path to /home/jim/files/some/deeply/nested/directory/or/other/my-project. Then, when you are in that directory, you can type z my-other-project and extend its path to /var/www/html/projects/top/secret/my-other.project.

Note: The first time I ran z and tried to change the directory, I got an error _z_dirs:2: no such file or directory: /home/jim/.z. This is a one-time event because z obviously needs to create a .z file in my home directory. If you have any problems, this article provides some suggestions.

  1. Using Git

zsh contains some basic Git integrations out of the box. For example, if you type git and press Tab, zsh will usefully output a large list of possible git commands. This will become more useful if you type git and then type the first few letters of the command you are looking for. For example git a Tabgenerates:

<code>alias myip="curl http://ipecho.net/plain; echo"</code>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

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

<code>sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"</code>
Copy after login
Copy after login
Copy after login

This provides a complete set of aliases, as well as some useful functions for handling Git. Some of the things I often use are:

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

You can also create alias for multiple commands at once by adding the following to the .zshrc file:

<code>sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"</code>
Copy after login
Copy after login

Now, when you are in the Git repository, typing acp "a commit message" will add all the changed files, commit them with any commit messages you specified, and then push them to your remote repository. The credit for this idea is to Ali Spittel.

  1. Get git repository status feedback now

At the beginning of the article, we activated a topic called avit. In the Git repository, this topic displays the branch name, along with a cross or checkmark indicating whether the branch is up to date. I like being able to see this information at a glance, which is definitely much better than typing git status all the time.

However, there are some topics that display this information in a more intuitive way. My favorite is Agnoster. Let's install it now.

In order for Agnoster to render arrows and Git icons correctly, you may need to install Powerline patched fonts. WSL users can do so by following the instructions in the troubleshooting section above. For macOS and Linux users, the installation instructions can be found in the project's repo.

Mac users on High Sierra or later may need to do some extra work to display these fonts. Unless you are using Catalina, this method is recommended. Catalina users need to install the patched font and select it in the terminal settings.

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

<code>git clone https://github.com/powerline/fonts.git</code>
Copy after login
Copy after login

The results should be shown in the image below.

10 Zsh Tips & Tricks: Configuration, Customization & Usage

After

, enable the theme by changing the following lines in the .zshrc file:

<code>alias myip="curl http://ipecho.net/plain; echo"</code>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Then restart the terminal. Once Agnoster is up and running, you will have a more intuitive understanding of the status of any Git repository. You will also receive visual feedback on whether the previous command succeeded or failed and whether you worked with elevated permissions.

10 Zsh Tips & Tricks: Configuration, Customization & Usage

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

  1. Plugins, plugins, plugins

As mentioned, Oh My Zsh comes with a large number of plugins. You really should browse these plugins and invest some time learning those that can help you get more productive. To help you get started, here are some popular plugins to watch out for:

  • – Upload files and pipeline content to the Cloudapp service. Output and copy the generated URL to the clipboard. cloudapp
  • : If the command is not recognized in command-not-found, this will use Ubuntu's command-not-found package to find it or suggest a typo (only for Ubuntu and openSUSE). $PATH
  • : Open the current version of Node API in your browser. node
  • : Add automatic completion function to npm. npm
  • : Pressing the sudo twice will precede the last command when the current command or command behavior is empty. sudo: Makes the interaction between the command line and the code editor easier.
  • vscode
Shortcut key combination
  1. This is not a unique skill for zsh, but for the sake of summary, I want you to understand these five time-saving shortcut key combinations.

Ctrl
  • U: Delete from cursor to the beginning of the line. Ctrl
  • K: Delete from cursor to end of line. Ctrl
  • W: Delete from the cursor to the beginning of the previous word. Alt
  • D: Delete from the cursor to the end of the next word. Ctrl
  • L: Clear the terminal. If you can remember these, you'll be surprised that they come in handy often.

Conclusion

In this article, I have demonstrated how to install zsh (Z shell) and set it as the default shell. I also showed how to make the most of your new terminal with Oh My Zsh framework. I hope that if you try these methods for a few weeks, you'll want to know how you've worked without them before.

And don't forget: If you have any questions about this, you can jump to the SitePoint forum and ask for help. I would also love to hear if I'm missing something—for example, your favorite theme, plugin, alias, or productivity tips.

You can contact me on the forum or on Twitter.

(The following is a pseudo-original creation of the original FAQs part, keeping the original intention unchanged and simplifying some repeated content)

Frequently Asked Questions about Zsh

  • How is Zsh different from other shells such as Bash? Zsh provides advanced Tab key automatic completion, spell checking, customizable prompts and more scripting options. Compared to traditional shells such as Bash, it aims to improve user experience and productivity.

  • Can I use Zsh on Windows? Yes, you can install Zsh on Windows through tools such as Cygwin, Windows Subsystem for Linux (WSL), or Git Bash. However, setting up Zsh on Windows may require some extra steps.

  • Can Zsh run my existing Bash script? Yes, Zsh can run Bash scripts, but there may be some compatibility issues due to the syntax differences between the two shells. It is best to verify and tweak the scripts to make sure they run correctly in Zsh.

  • What is the Zsh theme? Zsh theme determines the appearance of the terminal prompt. They usually display useful information, such as the current directory, Git status, etc. Oh My Zsh contains a variety of themes for you to choose from.

  • How to enable syntax highlighting in Zsh? You can enable syntax highlighting using Zsh plugins such as "zsh-syntax-highlighting". Install the plugin and add the activation command to your Zsh configuration file.

  • How to customize my Zsh prompt? You can customize the Zsh prompt by modifying the PS1 environment variable in the Zsh configuration file (usually ~/.zshrc). Alternatively, use frameworks like Oh My Zsh to easily select and customize themes.

  • What is a Zsh plugin? The Zsh plug-in is an extension that enhances Zsh functionality. They can provide additional features such as syntax highlighting, automatic suggestions, and improvements to specific commands or languages.

  • How to install Zsh plugin? Zsh plug-in managers such as "Oh My Zsh", "Zplug" and "Antigen" make it easy to install and manage plug-ins. Each manager has its own setup instructions, which usually involves modifying the Zsh configuration file.

  • What is Oh My Zsh? Oh My Zsh is a popular framework for managing Zsh configurations and plugins. It contains a collection of prebuilt themes and plugins that you can easily enable or customize.

  • How to set Zsh as my default shell? You can change the default shell to Zsh by running the command chsh -s $(which zsh). This will update shell preferences in system user configuration.

  • How to install Zsh? macOS and many Linux distributions usually have Zsh pre-installed. If not, you can install it using the system's package manager. For example, on a Debian-based system, you can run sudo apt-get install zsh.

  • What is Zsh (Z shell)? Zsh, or Z shell, is an advanced Unix shell that provides powerful customization options, improved scripting and interactive features for the command line interface.

  • How to configure and customize Zsh? Configuring and customizing Zsh involves editing .zshrc files, selecting themes and plugins (such as using Oh My Zsh), setting options, alias, and custom prompts (PS1). This needs to be adjusted according to personal preferences and needs.

The above is the detailed content of 10 Zsh Tips & Tricks: Configuration, Customization & Usage. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template