🇯🇵 日本語 | 🇺🇸 English | 🇪🇸 Español | 🇵🇹 Português | 🇹🇭 ไทย | 🇨🇳 中文

A Guide to Basic Zsh Commands and Convenient Features (Completion, History, Globbing)

In the last article, you installed Oh My Zsh, which should have significantly powered up your Zsh environment in both looks and functionality. While customization with aliases, themes, and plugins is a huge part of Zsh's appeal, the truth is, Zsh has many extremely powerful and convenient features built-in, without relying on any of those external tools.

For many people who switch from other shells like Bash, the reason they fall in love with Zsh is the intelligence and thoughtfulness of these "standard features." It feels like having a highly competent secretary constantly by your side, anticipating and supporting your work.

This article will focus on three powerful pillars that Zsh has from the start: "Completion," "History," and "Globbing (Filename Expansion)," and explain their specific usage. We promise that by mastering these features, your command-line work will become another level faster and more comfortable!


1. The "Predictive Text" Level of Smartness! Zsh's "Completion Feature"

If there's one killer feature of Zsh, it has to be its "completion feature." Just by pressing the Tab key, it provides surprisingly intelligent support that goes far beyond simple filename completion.

Completing Command Options

You no longer need to check the manual thinking, "I wonder what options were available for this command..." Just type the command followed by a - and press the Tab key, and it will display a list of available options.

$ ls -[Press Tab]
--all                             -A --almost-all                 -B --ignore-backups
--author                          -d --directory                  -D --dired
--color                           -F --classify                   -G --no-group
...(and so on)...

Furthermore, you can select a candidate with the arrow keys and it will even display an explanation of what each option does.


Switching Completion Candidates Based on Context

Zsh understands what the command you're currently typing is asking for and intelligently switches the completion candidates.

For example, if you press the Tab key after the kill command, which terminates a process, it will display a list of currently running process names as candidates, not filenames.

$ kill [Press Tab]
101 zsh      102 login    103 top      104 sshd
...

2. The "It Just Gets It" History Feature

The history feature, which reuses previously executed commands, is also far smarter in Zsh than in Bash.

Incremental History Search

When you're thinking, "I want to use that long git command I typed before...", in Zsh, you can just type the beginning of the command (e.g., git) and then press the ↑ (up arrow) key.

This will search backward through your command history, but only through the commands that start with git, not the entire history. This allows you to find the command you're looking for overwhelmingly faster.

Re-running a Command with `!!`

!! is a special alias that expands to the last command you executed. This shines the brightest when you forget to add sudo.

$ apt update
E: Could not open lock file ... Permission denied
$ sudo !!
sudo apt update
...

Just by typing sudo !!, it re-executes the command as sudo apt update. The time-saving effect of this is enormous.


3. The Master of File Selection, "Globbing"

Globbing is a feature that uses wildcards like * to specify multiple files at once. In Zsh, this feature is greatly expanded and is called "Extended Globbing." (To use it, the setting setopt EXTENDED_GLOB is required. If you're using Oh My Zsh, it's often enabled by default).

Recursive File Search (**)

Using **, you can search for files deep within subdirectories with a single command.

# List all .js files in all subdirectories
ls -l **/*.js

OR Search (|)

You can use | to specify files with an "A or B" condition.

# List .jpg or .png files
ls *.(jpg|png)

NOT Search (^)

You can use ^ to specify "all files except for X."

# List everything except for .log files
ls ^*.log

Specifying a Numerical Range (<->)

This is useful for specifying a particular range of sequentially numbered files.

# List files from image1.jpg to image10.jpg
ls image<1-10>.jpg

Conclusion

This time, we've introduced some of the standard, convenient features that are the reason why Zsh is called the "ultimate shell."

While each of these features may seem like a small improvement on its own, they add up in your daily work to greatly increase your productivity. Zsh is not just a tool for executing commands; it's a "partner" that actively supports your work.

Now, for those who are seriously considering migrating from Bash to Zsh, the next article will explain compatibility points to watch out for when running Bash scripts in Zsh, and a summary of checkpoints to make the transition smooth.

A Compatibility Checklist for People Switching from Bash to Zsh