Zsh

From ArchWiki
Revision as of 10:46, 15 July 2011 by Kynikos (talk | contribs) (Configuration files: I was thinking in plural)
Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Zsh is a powerful shell that operates as both an interactive shell and as a scripting language interpreter. While being compatible with Bash (not by default, only if you issue "emulate sh"), it offers many advantages such as:

  • Faster
  • Improved tab completion
  • Improved globbing
  • Improved array handling
  • Fully customisable

The Zsh FAQ offers more reasons to use Zsh as your shell.

Installation

Before starting you may want to see what shell is currently being used:

$ echo $SHELL

Install the Template:Package Official package available from [extra].

Initial configuration

Make sure that Zsh has been installed correctly by running the following in a terminal:

$ zsh

You should now see zsh-newuser-install, which will walk you through some basic configuration. If you want to skip this, press q.

Making Zsh your default shell

If the shell is listed in Template:Filename you can use the chsh command to change your default shell without root access. If you installed Zsh from [extra] it should already have an entry in Template:Filename.

Change the default shell for the current user:

$ chsh -s $(which zsh)
Note: You have to logout and log back in, in order to start using Zsh as your default shell.

After logging back in, you should notice Zsh's prompt, which by default looks different from Bash's. However you can verify that Zsh is the current shell by issuing:

$ echo $SHELL
Tip: If you are replacing bash, you may want to move some code from Template:Filename to Template:Filename (e.g. the prompt and the aliases) and from Template:Filename to Template:Filename (e.g. the code that starts your X Window System). See the Configuration section for details.

Configuration files

Although Zsh is usable out of the box, it is almost certainly not set up the way you would like to use it, but due to the sheer amount of customisation available in Zsh, configuring Zsh can be a daunting and time-consuming experience.

At login, Zsh sources the following files in this order:

Template:Filename
This file is sourced by all Bourne-compatible shells upon login: it sets up an environment upon login and application-specific (/etc/profile.d/*.sh) settings.
Template:Filename
This file should contain commands to set the command search path, plus other important environment variables; it should not contain commands that produce output or assume the shell is attached to a tty.
Template:Filename
This file is generally used for automatic execution of user's scripts.
Template:Filename
This is Zsh's main configuration file.
Template:Filename
This file is generally used for automatic execution of user's scripts.

At logout it sources Template:Filename, which is used for automatic execution of user's scripts.

~/.zshrc configuration

Included below is a sample configuration file, it provides a decent set of default options as well as giving examples of many ways that Zsh can be customised. In order to use this configuration save it as a file named Template:Filename. You can then apply the changes without needing to logout and then back in by running:

$ source ~/.zshrc

Simple .zshrc

Here is a simple Template:Filename, that should be sufficient to get you started:

Template:File

Command Completion

Perhaps the most compelling feature of Zsh is its advanced autocompletion abilities. At the very least, you will want to enable autocompletion in your Template:Filename. To enable autocompletion, add the following to:

Template:File

The above configuration includes ssh/scp/sftp hostnames completion but in order for this feature to work you will need to prevent ssh from hashing hosts names in ~/.ssh/known_hosts (Warning: be aware that this makes your computer vulnerable to "Island-hopping" attacks). In that intention, comment the following line or set the value to "no": Template:File And move your ~/.ssh/known_hosts somewhere else so that ssh creates a new one with with un-hashed hostnames (warning: previously known hosts will thus be lost).

For autocompletion with an arrow-key driven interface, add the following to: Template:File

For autocompletion of command line switches for aliases, add the following to: Template:File

Key Bindings

Zsh doesn't use readline, instead it uses its own and more powerful zle. It doesn't read Template:Filename or Template:Filename. zle has an emacs mode and a vi mode. By default, it tries to guess whether you want emacs or vi keys from the $EDITOR environment variable. If it's empty, it will default to emacs. You can change this with bindkey -v or bindkey -e.

To get some special keys working: Template:File

Alternatively, you can convert /etc/inputrc for Zsh: Template:File

Note: To get the proper sequences for certain key combinations, start Template:Codeline or Template:Codeline without any parameters and press them; they should then be printed in the terminal. Both can be closed again via Ctrl+c.

History search

You can add these lines to your .zshrc

Template:File

Doing this, only past commands beginning with the current input would've been shown.

Prompts

There is a quick and easy way to set up a colored prompt in Zsh. Make sure that prompt is set to autload in your Template:Filename. This can be done by adding these lines to:

Template:File

You can now see available prompts by running the command:

$ prompt -l

To try one of the commands that is listed, use the command prompt followed by the name of the prompt you like. For example, to use the "walters" prompt, you would enter:

$ prompt walters

Customizing your prompt

In case you are dissatisfied with the prompts mentioned above(or want to expand their usefulness), zsh offers the possibility to build your own custom prompt. Zsh supports a left- and right-sided prompt additional to the single, left-sided prompt that is common to all shells. To customize it, the following variables can be used:

Prompt variables

General
 %n 
The username
 %m 
The computer's hostname(truncated to the first period)
 %M 
The computer's hostname
 %l 
The current tty
 %? 
The return code of the last-run application.
 %# 
The prompt based on user privileges (# for root and % for the rest)
Times
 %T 
System time(HH:MM)
 %* 
System time(HH:MM:SS)
 %D 
System date(YY-MM-DD)
Directories
 %~ 
The current working directory. If you are in you are in your $HOME, this will be replaced by "~".
 %d 
The current working directory.

For the options mentioned above: You can prefix an integer to show only certain parts of your working path. If you entered %1d and found yourself in /usr/bin it would show bin. This can also be done with negative integers: %-1d using the same directory as above would show /.

Formatting
 %U [...] %u 
Begin and end underlined print
 %B [...] %b 
Begin and end bold print
 %{ [...] %} 
Begin and enter area that will not be printed. Useful for setting colors.
Colors

Zsh has a different approach to setting colors on the terminal than the one depicted here. First you write in your .zshrc:

autoload -U colors && colors

Following commands would now produce the color escape sequence needed to set the requested color when the prompt is printed:

$fg[color] 
will set the textcolor(red,green,blue, etc)
$reset_color 
will reset the textcolor to white

It is useful to put these commands in %{ [...] %}.

Example

To have a two-sided prompt you coud write:

PROMPT="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%1~ %{$reset_color%}%#"
RPROMPT="[%{$fg[yellow]%}%?%{$reset_color%}]"

It would equal(without colors):

username@host ~ %                                                         [0]

Advanced .zshrc

This is an example of a more advanced Template:Filename:

Template:File

There are many more ways that you can customise Zsh, obviously far too many to list here, see the Zsh manual for more information.

Sample .zshrc files

An Arch package named grml-zsh-config comes from http://grml.org/zsh and provides a zshrc file that includes many tweaks for your zshell.

Here is a list of Template:Filename files. Feel free to add your own:

Global configuration

Tango-inaccurate.pngThe factual accuracy of this article or section is disputed.Tango-inaccurate.png

Reason: please use the first argument of the template to provide a brief explanation. (Discuss in Talk:Zsh#)

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: please use the first argument of the template to provide a brief explanation. (Discuss in Talk:Zsh#)

Occasionally you might want to have some settings applied to all zsh users regardless of their individual configuration file. The zsh wiki tells us that there's some global configuration files, for example /etc/zshrc. This however is not the case with ArchLinux since it has been compiled with flags specifically to target /etc/zsh instead.

So, for global configuration it's /etc/zsh/zshrc. Not /etc/zshrc.

Same goes for zlogout, zlogin and zprofile.

Uninstallation

If you decide that Zsh is not the shell for you and you want to return to Bash, you must first change your default shell back to Bash, before removing the Zsh package.

Follow, Zsh#Making Zsh your default shell to change the default shell back to Bash, just replace zsh with bash.

Now you can safely remove the Zsh package.

Warning: Failure to follow the above will result in all kinds of problems.

If you did not follow the above, you can still change the default shell back to Bash by editing /etc/passwd as root. For example:

from:

username:x:1000:1000:Full Name,,,:/home/username:/bin/zsh

to:

username:x:1000:1000:Full Name,,,:/home/username:/bin/bash

External Resources

  • IRC channel: #zsh at irc.freenode.org