Zsh

From ArchWiki

Jump to: navigation, search
i18n
English
Česky

Zsh is a powerful shell that operates as both an interactive shell and as a scripting language interpreter. While being similar to Bash, 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.

Contents

Installation Instructions

Before starting lets find out what shell is currently being used:

$ echo $SHELL

Installing

To install the package for Zsh, run:

# pacman -S zsh

Before proceeding with the next step to make Zsh your default shell, you should ensure that it has been installed correctly by running Zsh in a xterm or in a tty:

$ zsh

If the installation has gone smoothly you should now find yourself staring at a rather unfamiliar prompt, for now just type exit:

$ exit

Making Zsh your default shell

To change a user's default shell without root access, the chsh command is used. The chsh command can be used to change a user's default shell without root access if the shell is listed in /etc/shells. If you installed Zsh using pacman, Zsh should already have an entry in /etc/shells.

To proceed you need to know the full path for Zsh, so run:

$ which zsh

Change the default shell for the current user:

$ chsh -s /bin/zsh

An alternative way to change a user's default shell is with the command usermod. The disadvantage of this method is that you must have root access. But multiple users default shell can be changed quickly as root, using usermod or chsh.

Change the default shell for multiple users, using usermod:

# usermod -s /bin/zsh username

Change the default shell for multiple users, using chsh:

# chsh -s /bin/zsh username
Note: The user needs to logout and log back in, to start using Zsh as their default shell.

After logging back in, the user can verify that Zsh is their default shell by:

$ echo $SHELL

If you do not have root access, and Zsh is not listed in /etc/shells but you would still like to use Zsh as your default shell, see this entry in the Zsh FAQ.

Configuration

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, creating a Zsh config can be a daunting and time-consuming experience.

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 .zshrc. You can then apply the changes without needing to logout and then back in by running:

$ source ~/.zshrc

Simple .zshrc

Here is a simple .zshrc, that should be sufficient to get you started:

File: ~/.zshrc
autoload -U compinit promptinit
compinit
promptinit
 
# This will set the default prompt to the walters theme
prompt walters

/etc/zprofile

Much like the /etc/profile used for Bash, this file is for global Zsh settings & is a good place from which to run scripts from /etc/profile.d/ and set up environment variables such as $PATH. When setting up $PATH etc. in .zshrc and using a login-manager such as kdm, you may find those settings not being taken up by the window-manager, whereas they are when using /etc/zprofile.

Generally speaking, you can copy /etc/profile to /etc/zprofile.

An example configuration:

File: /etc/zprofile
###############
# Zsh profile #
###############
 
export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/X11R6/bin:/opt/bin"
 
export MANPATH="/usr/man:/usr/X11R6/man"
export LESSCHARSET="latin1"
export LESS="-R"
 
# load profiles from /etc/profile.d
#  (to disable a profile, just remove execute permission on it)
if [ `ls -A1 /etc/profile.d/
Note: Zsh's PKGBUILD defines that profile is in /etc/profile, so reference to /etc/zprofile is a bit misleading...

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 .zshrc. To enable autocompletion, add the following to:

File: ~/.zshrc
autoload -U compinit
compinit

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

File: ~/.zshrc
zstyle ':completion:*' menu select

Key Bindings

Zsh doesn't read /etc/inputrc, which tells the shell what commands sent by the terminal emulator mean. To have some standard key bindings working on Zsh, add something like this to:

File: ~/.zshrc
# key bindings
bindkey "\e[1~" beginning-of-line
bindkey "\e[4~" end-of-line
bindkey "\e[5~" beginning-of-history
bindkey "\e[6~" end-of-history
bindkey "\e[3~" delete-char
bindkey "\e[2~" quoted-insert
bindkey "\e[5C" forward-word
bindkey "\eOc" emacs-forward-word
bindkey "\e[5D" backward-word
bindkey "\eOd" emacs-backward-word
bindkey "\e\e[C" forward-word
bindkey "\e\e[D" backward-word
bindkey "^H" backward-delete-word
# for rxvt
bindkey "\e[8~" end-of-line
bindkey "\e[7~" beginning-of-line
# for non RH/Debian xterm, can't hurt for RH/DEbian xterm
bindkey "\eOH" beginning-of-line
bindkey "\eOF" end-of-line
# for freebsd console
bindkey "\e[H" beginning-of-line
bindkey "\e[F" end-of-line
# completion in the middle of a line
bindkey '^i' expand-or-complete-prefix

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 .zshrc. This can be done by adding these lines to:

File: ~/.zshrc
autoload -U promptinit
promptinit

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

Advanced .zshrc

This is an example of a more advanced .zshrc:

File: ~/.zshrc
###########################################################        
# Options for Zsh

export HISTFILE=~/.zsh_history
export HISTSIZE=50000
export SAVEHIST=50000
eval `dircolors -b`

autoload -U compinit compinit
setopt autopushd pushdminus pushdsilent pushdtohome
setopt autocd
setopt cdablevars
setopt ignoreeof
setopt interactivecomments
setopt nobanghist
setopt noclobber
setopt HIST_REDUCE_BLANKS
setopt HIST_IGNORE_SPACE
setopt SH_WORD_SPLIT
setopt nohup

# PS1 and PS2
export PS1="$(print '%{\e[1;34m%}%n%{\e[0m%}'):$(print '%{\e[0;34m%}%~%{\e[0m%}')$ "
export PS2="$(print '%{\e[0;34m%}>%{\e[0m%}')"

# Vars used later on by Zsh
export EDITOR="gvim -geom 82x35"
export BROWSER=links
export XTERM="aterm +sb -geometry 80x29 -fg black -bg lightgoldenrodyellow -fn -xos4-terminus-medium-*-normal-*-14-*-*-*-*-*-iso8859-15"

##################################################################
# Stuff to make my life easier

# allow approximate
zstyle ':completion:*' completer _complete _match _approximate
zstyle ':completion:*:match:*' original only
zstyle ':completion:*:approximate:*' max-errors 1 numeric

# tab completion for PID :D
zstyle ':completion:*:*:kill:*' menu yes select
zstyle ':completion:*:kill:*' force-list always

# cd not select parent dir
zstyle ':completion:*:cd:*' ignore-parents parent pwd

##################################################################
# Key bindings
# http://mundy.yazzy.org/unix/zsh.php
# http://www.zsh.org/mla/users/2000/msg00727.html

typeset -g -A key
bindkey '^?' backward-delete-char
bindkey '^[[1~' beginning-of-line
bindkey '^[[5~' up-line-or-history
bindkey '^[[3~' delete-char
bindkey '^[[4~' end-of-line
bindkey '^[[6~' down-line-or-history
bindkey '^[[A' up-line-or-search
bindkey '^[[D' backward-char
bindkey '^[[B' down-line-or-search
bindkey '^[[C' forward-char 

##################################################################
# My aliases

# Set up auto extension stuff
alias -s html=$BROWSER
alias -s org=$BROWSER
alias -s php=$BROWSER
alias -s com=$BROWSER
alias -s net=$BROWSER
alias -s png=feh
alias -s jpg=feh
alias -s gif=feg
alias -s sxw=soffice
alias -s doc=soffice
alias -s gz=tar -xzvf
alias -s bz2=tar -xjvf
alias -s java=$EDITOR
alias -s txt=$EDITOR
alias -s PKGBUILD=$EDITOR

# Normal aliases
alias ls='ls --color=auto -F'
alias lsd='ls -ld *(-/DN)'
alias lsa='ls -ld .*'
alias f='find |grep'
alias c="clear"
alias dir='ls -1'
alias gvim='gvim -geom 82x35'
alias ..='cd ..'
alias nicotine='/home/paul/downloads/nicotine-1.0.8rc1/nicotine'
alias ppp-on='sudo /usr/sbin/ppp-on'
alias ppp-off='sudo /usr/sbin/ppp-off'
alias firestarter='sudo su -c firestarter'
alias mpg123='mpg123 -o oss'
alias mpg321='mpg123 -o oss'
alias vba='/home/paul/downloads/VisualBoyAdvance -f 4'
alias hist="grep '$1' /home/paul/.zsh_history"
alias irssi="irssi -c irc.freenode.net -n yyz"
alias mem="free -m"
alias msn="tmsnc -l hutchy@subdimension.com"

# command L equivalent to command |less
alias -g L='|less' 

# command S equivalent to command &> /dev/null &
alias -g S='&> /dev/null &'

# type a directory's name to cd to it
compctl -/ cd

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

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

Enable Unicode (obsolete)

Note: Unicode is enabled by default on the official Arch Linux Zsh package.

The latest versions of Zsh support unicode characters. To enable it, just rebuild the package with the Arch Build System.

And add the option --enable-multibyte to:

File: PKGBUILD
build() {
cd $startdir/src/$pkgname-$pkgver
./configure --prefix=/usr --bindir=/bin \
--enable-etcdir=/etc/zsh \
--enable-zshenv=/etc/zsh/zshenv \
--enable-zlogin=/etc/zsh/zlogin \
--enable-zlogout=/etc/zsh/zlogout \
--enable-zprofile=/etc/profile \
--enable-zshrc=/etc/zsh/zshrc \
--enable-maildir-support \
--with-curses-terminfo \
--enable-zsh-secure-free \
--enable-multibyte

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
Personal tools