Linux

Learn the power features of zsh

Vincent Danen describes some of the features of the Z shell, such as tab-completion and prompt-handling.

The Z Shell (zsh) is a power-shell that is not often used by many Linux users. The reason for this is that most Linux distributions install, and make default, the bash shell. zsh is packaged for virtually every Linux distribution and installation is usually an apt-get, urpmi, or yum away.

One of the great features of zsh is tab-completion; it also handles all the logistics of tab-completion and is extremely easy to implement, just by adding two lines to your ~/.zshrc file:

autoload -U compinit
compinit

The compinit function is what loads the tab-completion system by defining a shell function for every utility that zsh is able to tab-complete. By using autoload, you can optimize zsh by telling it to defer reading the definition of the function until it's actually used, which speeds up the zsh startup time and reduces memory usage.

Using the setopt command, you can configure over 150 different options that impact how zsh works. For instance:

setopt autocd

The line above will allow you to change directories simply by typing the name of the directory (no need to use cd). Or, you might wish to use more powerful globbing or pattern matching features, which can be done by adding the line below to ~/.zshrc:

setopt extended_glob

The various zsh options that can be set with setopt are documented in the zshoptions manpage:

$ man zshoptions

Note that the ~/.zshrc file is sourced for both interactive and login shells. If you want, to set options for when zsh is run non-interactively (i.e., via a cronjob), then you'll want to add those to ~/.zshenv.

Another nice feature with zsh is how it handles prompts. These can be custom or they can be loaded via zsh's prompt system, which contains a number of "stock" prompts that might be suitable. For instance, to use the prompt system enter:

autoload -U promptinit
promptinit
prompt fire

To list the available fonts, on the command-line, enter "prompt -l". To define your own prompt, use the $PS1 variable, but zsh uses different format specifiers than bash, so a nice prompt might look like:

PS1=$'%{\e[1;32m%}%n@%m%{\e[0m%}:%B%~/%b >%# '

The resulting prompt looks like:

joe@odin:~/ >%

with the user and hostname in bright green.

Delivered each Tuesday, TechRepublic's free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

10 comments
dgary
dgary

I've been using custom prompts in bash for... decades? and tab completion is in just about every shell I know of

ds4211a
ds4211a

This article ought to really make Windows users switch to Linux. I hardly ever use the command line on Windows anymore. DOS was pretty much done away with for the average Windows user with Windows 98. Maybe Linux will catch up some day.

Tony K
Tony K

How does the tab-completion compare to Bash? For example, with Bash tab-completion, I can tell it how to cycle through my sshhosts file, so I could type "ssh -> tab -> server1 -> tab -> server2" etc.

Tony K
Tony K

I can only assume that you being a "PC Support" guy is just a cover because you speak like that average user you describe. The command line didn't die with Windows 95, it's just as important as it was when there was an underlying DOS system to Windows. Real tech guys know there's infinitely more to Windows than what you can click on.

TravisFx
TravisFx

Boy its still so easy to catch a penguin isn't it? They'll snap at anything! T

cjc5447
cjc5447

Your post must be a troll, or you are an extraordinally clueless moron. Or both.

ds4211a
ds4211a

Why thank you for your enlightened response. Calling someone names is so much more intelligent than making a reasoned reply. I have tried numerous versions of Linux starting with Red Hat 5.0 which I installed for a class project. I have installed two or three versions of Mandrake Linux, a few versions of Red Hat Linux, TurboLinux, SuSE Linux, Ubuntu, and Xandros Linux since that first installation. I have also enjoyed running the live DSLinux CD. But I always seem to go back to using some version of Windows because I don't have to learn a bunch of new commands. I learned DOS a long time ago. I still use DOS from time to time. But most of the time I prefer to use the graphical interface. So I guess that makes me a Troll and a Moron. I thought calling someone a Moron ended in about the 6th grade. Which reminds me of the Redneck joke about the father and the son being in the same grade in elementary school. By the way, do you copy your son's homework??

DanLM
DanLM

But, after I posted what I did. I realized the command prompt offers command line. I do not have a good knowledge of dos/command prompt though. I didn't know about the other things you listed, and I apologize for the miss post. Dan

Tony K
Tony K

Then, you should open the command prompt on your Windows box. All of the examples you gave are easily doable on the command line on Windows, too. :) You can also install Cygwin and have access to the same tools you use on your Unix box in Windows. You can install the Resource Kits (they're free to download from MS, they don't cost money anymore. Haven't since NT4) and gain even more power. Finally, you can use the new Monad shell or code stuff in Vbscript. Very easy to learn vbscript, and there isn't anything you can't do with it.

DanLM
DanLM

The thing that I appreciate most is the command line. I do not like or trust powerful features being executed without an understanding of what is occurring. Using command line offers you so much more flexibility in performing various tasks that windows never could achieve without over complicating the matter. Example: Finding all files owned by user dan across the whole machine and changing the security attributes. Or, only doing this same task from the current position in a tree to regress only 3 levels. Both case's being one line of command line. This is just an example of the flexibility that command line offers that a window environment would be hard pressed to replicate without increasing the complexity of it or number of steps required to perform same task. This same type of thing could be used for changing file names also. Just an example. Shoot, even copying and painting is easier with command line. By the way, I also use and like windows. But windows can not compete with command line utilities, not even close. It's not it's strong point. Dan

Editor's Picks