Zsh
From ArchWiki
Contents |
[edit] Introduction
Zsh is a powerful shell that operates as both an interactive shell and as a scripting language interpreter. While being compatible with 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.
[edit] Installation instructions
To install the package for zsh using pacman 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 an xterm. If the installation has gone smoothly you should now find yourself staring at a rather unfamiliar prompt, for now just type exit.
To change a user's shell without root access, the chsh command is used. The commmand chsh can only be used to change a user's 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 to the zsh executable, so run
which zsh
Shown here as an example is the command I used to change my shell; substitute my values for your own.
chsh -s /bin/zsh paul
An alternative way to change a user's loging shell is with the command usermod. The disadvantage of this method is that you must have root access to run usermod. The syntax for this command is
usermod -s /bin/zsh paul
You will now need to logout and log back in, and you should find yourself in zsh instead of bash.
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.
[edit] 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
[edit] Simple .zshrc
Here is a simple .zshrc, that should be sufficient to get you started:
autoload -U compinit promptinit compinit promptinit # This will set the default prompt to the walters theme prompt walters
[edit] /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, but here's an example /etc/zprofile :
Note: zsh's PKGBUILD defines that profile is in /etc/profile, so reference to /etc/zprofile is a bit misleading...
###############
# 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 INPUTRC="/etc/inputrc"
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/ | wc -l` -gt 0 ]; then
for profile in /etc/profile.d/*.sh; do
if [ -x $profile ]; then
. $profile
fi
done
unset profile
fi
# Locale settings (find your locale with 'locale -a')
export LANG="en_GB.utf8"
export LC_COLLATE="C"
umask 022
[edit] 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 your .zshrc
autoload -U compinit compinit
[edit] 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 .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
[edit] 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 the lines:
autoload -U promptinit promptinit
to your .zshrc. 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
[edit] Advanced .zshrc
This is an example of a more advanced .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.
[edit] Sample .zshrc files
Here is a list of .zshrc files. Feel free to add your own:
- Øyvind 'Mr.Elendig' Heggstad <=> Basic setup, with dynamic prompt and window title/hardinfo <=> http://arch.har-ikkje.net/configs/home/dot.zshrc
- dotfiles.org's .zshrc repository: contains many .zshrc files from contributing users, feel free to upload your own
[edit] Enable Unicode
NOTE: Unicode is enabled by default on the official Arch Linux zsh package for a while now.
The latest versions of zsh support unicode characters. To enable it, just rebuild the package with abs. First you have to update the abs tree.
$ su root $ abs
Then go to /var/abs/extra/system/zsh and add the option --enable-multibyte to the 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
...
Now you can rebuild the package and update it.
$ makepkg $ pacman -U 'new zsh package'
[edit] 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 before removing the package. Failure to do so will result in all kinds of problems.
First you need to find out the path to sh, do this by running
which sh
Then, as with the install, use the usermod program to modify your default shell
usermod -s /bin/sh paul
You are then free to remove the zsh package using pacman
pacman -R zsh
[edit] External Resources
- IRC channel: #zsh at irc.freenode.org