Oct 182010
 

zshzsh it’s a perfect alternative to the classic bash that we have as default on our linux box.

zsh is a shell designed for interactive use, although it is also a powerful scripting language. Many of the useful features of bash, ksh, and tcsh were incorporated into zsh; many original features were added.

Origin

The first version of zsh was written by Paul Falstad in 1990 when he was a student at Princeton University.

Etymology

The name zsh derives from Yale professor Zhong Shao, then a teaching assistant at Princeton University. Paul Falstad thought that Shao’s login name, “zsh”, was a good name for a shell.



installation

zsh is available in most distributions, use your packet manager to install it, in ubuntu use: aptitude install zsh

First run

The first time you run zsh you will get this output:


This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory~). This function can help you with a few settings that shouldmake your use of the shell easier.
You can:
(q) Quit and do nothing. The function will be run again next time.
(0) Exit, creating the file ~/.zshrc containing just a comment.That will prevent this function being run again.
(1) Continue to the main menu.
(2) Populate your ~/.zshrc with the configuration recommended by the system administrator and exit (you will need to edit the file by hand, if so desired).

Going to the main menu presents several options on how to personalize history, prompts, and other things, I suggest you use the 0, I will present some of the recommended configurations.

Features

Tab completion

The Z shell (zsh) pioneered the support for fully programmable completion, allowing users to have the shell automatically complete the parameters of various commands unrelated to the shell itself, which is accomplished by priming the shell with definitions of all known switches as well as appropriate parameter types. This allows the user to e.g. type tar xzf Tab ⇆ and have the shell complete only tarred gzip archives from the actual filesystem, skipping files which are incompatible with the input parameters. A modern zsh installation comes with completion definitions for over five hundred commands.

To enable the tab-completion system, you need to run the following commands in the zsh shell:

% autoload -U compinit
% compinit

And to have a better output use:

% zstyle ':completion:*:descriptions' format '%U%B%d%b%u'
% zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b'

Prompt selection

Prompt selectionprompt

Zsh has some built in “theme” for prompt, to see and use them you first, need to initialize the advanced prompt support:

% autoload -U promptinit
% promptinit

Available prompts samples may be printed via the prompt -p command. Now we can activate our favorite one for example:

prompt elite2 yellow

History customization

This aspect is also highly customizable, decide to write the history in a file with name .histfile, keep 1000 command in the current shell and save the last 1000 at the closure of the terminal.
This is translated with these settings:

HISTFILE=~/.histfile
HISTSIZE=1000
SAVEHIST=1000

We want also to don’t save duplicates entry or commands that start with a space:

setopt hist_ignore_all_dups hist_ignore_space

Auto cd with 1 command

Zsh also allows you to go into a directory simply by typing its path when you enable an option, so to move in /etc/init.d in zsh you can write the path without the cd in front, enable the option:

setopt autocd

Extended Globbing wildcard

Globbing is a way of expanding wildcard characters in a non-specific file name into a set of specific file names. Shells use globbing all the time. For instance:

$ ls foo*
foo1 foo2

zsh is one of the most powerful shells with some very interesting wildcard characters that can be used. For instance, you can use glob qualifiers, which are characters with a special meaning. If you wanted to list all the symbolic link files in the current directory you would use:

$ ls *(@)

Some other zsh glob qualifiers include a forward slash (/) for directories and a period (.) for plain files; the zshexpn man page has a full list. If there are no matches, zsh will return an error; if there is nothing to pass to the ls program (or any other program), the program will not even be called by zsh.

There are other useful patterns, such as the (**/) pattern. What this does is tell zsh to match any number of directories to any depth, including the current directory. To find all the (*.sh) and (*.py) files in the current directory and sub-directories in a list suitable for parsing (i.e., perhaps in a script), you would use:

$ ls -1 **/*.(sh|py)

Finally, to enable extended globbing at all times, add this to ~/.zshrc:

setopt extended_glob

.zshrc

This is the file with some of the option we have saw in this article

# Auto completion
autoload -Uz compinit
compinit
zstyle :compinstall filename '/home/capecchi/.zshrc'
zstyle ':completion:*:descriptions' format '%U%B%d%b%u'
zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b'
#Prompt setup
autoload -U promptinit
promptinit
prompt elite2 yellow
# History
HISTFILE=~/.histfile
HISTSIZE=1000
SAVEHIST=1000
#We set some options here
setopt appendhistory autocd hist_ignore_all_dups hist_ignore_space

Making zsh default shell

A method of changing shells is to use the chsh command (a utility used to change a user’s login shell). A normal user may only change the login shell for his own account. As root, you can change the login shell of any user.

chsh -s /bin/zsh username

Popular Posts:

Flattr this!

  2 Responses to “introduction to zsh”

  1. hi

    in your example. zshrc last line
    [Code]
    setopt appendhistory autocd hist_ignore_all_dups hist_ignore
    [/ Code]

    missing ‘_space’
    I changed the last line so
    [Code]
    setopt appendhistory autocd hist_ignore_all_dups hist_ignore_space
    [/ Code]

    Erika

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*