Tutorials‎ > ‎

Introduction to zsh

Introduction

You might ask yourself why you should ever use a different than the default shell on your operating system. The most GNU/Linux distributions come up with bash allowing tab-completion, aliases, file globbing and more. Everything that an average shell user needs to be happy. So why install another shell? The Z-Shell (or zsh) features a far more customizable tab-completion, extended globbing, different kinds of aliases, spelling correction and much more. If you are a bash power user, give it a try.

Installation

The zsh package needs to be installed on your system. On Debian-based systems, just type the following command:

% sudo apt-get install zsh

First start

You can start the Z-Shell like any other application on your system. Just type the following command:

% zsh

At the moment you can't see any big differences between your current shell and the zsh. It even doesn't allow tab-completion at all. We'll take care about this later.

You should make zsh to your default shell if you don't want to start it manually every time you want to use it. If you're unhappy with it, you can change this back to your current shell. To change your default shell to zsh, type the following command:

% chsh -s /bin/zsh

Type in your password and you're done.

Configuration

The shell is configured by a file called ~/.zshrc. For a good start-off, you can use my version of this file. Just type these commands into your terminal and the file gets copied to the right place.

% wget -O ~/.zshrc http://sites.google.com/site/apipetohell/files/.zshrc
% source ~/.zshrc

As you can see, the prompt has changed to ~$. I find it better than the default one. Also, you're equipped with a well-configured tab-completion system and some aliases that I find useful.

Now, try it. Type

% kill <TAB><TAB>

into your terminal. Pay attention to the blank right after the command. If you forget it, the interpreter will give you a list of available commands instead of the expected list of currently running processes.

Aliases

zsh offers you three kinds of aliases: Normal ones, global aliases and suffixes. Normal aliases are the same as in bash. Global aliases can not only be used at the beginning of a command, but can be used in the whole command. With suffixes you can associate file types with certain applications.

The following alias allows you to quickly open less with the contents of a command.

% alias -g L="| less"

Now try this command:

% ls L

As you can see, less was opened with the contents of the ls command. This alias and many others are already defined in my ~/.zshrc file (see above).

Extended Globbing

You probably know the asterisk (*) and you might know the question mark (?). These characters are also called wildcards and are used for path expansion which you might know as globbing.

Following examples will also work in bash.

% ls *.png            # all png files (e.g. blahblah.png, ...)
% ls ?.png            # png files with one char (e.g. a.png, b.png, ...)
% ls ??.png           # png files with two chars (e.g. 01.png, 02.png, ...)

You can also use squared brackets ([ ]) to be more specific:

% ls [A-Z]*.png       # all png files starting with a capital (e.g. Blahblah.png, ...)
% ls image-[0-9].png  # e.g. image-0.png, image-1.png, ...

But what if you are in a directory where you can not distinguish between files and folders, because many files don't have an extension? This is where extended globbing comes to play.

% ls *(/)             # all directories
% ls *(.)             # all files

Now you can do very precise globbing:

% ls [A-Z]*(/)        # all directories starting with a capital
% ls [0-9]*(.)        # all files starting with a number

There's also a feature named recursive globbing. You can search files in the current directory and in all sub-directories.

% ls **/*(.)          # all files in the current directory and in all
                      # sub-directories
% ls **/*.jpg         # all jpg files in the current directory and in all
                      # sub-directories

Resources


The gadget you added is not valid
Comments