Zsh

From ArchWiki
Revision as of 15:50, 3 August 2005 by Yyz (Talk | contribs) (initial creation)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction

Zsh is a powerful shell that operates as both as 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.

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 users shell the usermod command is used, so root access is required. 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, substitue my values for your own.

usermod -s /usr/local/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 but 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.

For this reason I have included below 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

My .zshrc:

###########################################################       
# Important sutff.

# Export some vars
export PATH="$PATH:/usr/local/bin:/usr/local/jdk/jdk1.5.0_04/bin:/opt/kde/bin:/usr/bin:/bin:/usr/X11R6/bin:/usr/games"
export MINICOM="-c on"
export MANPATH=/usr/local/man:/usr/man:/usr/X11R6/man
export HOSTNAME="`cat /etc/HOSTNAME`"
export LESSOPEN="|lesspipe.sh %s"
export LESS="-M"
export CLASSPATH=".:/usr/local/jdk/jdk1.5.0_04/jre/lib/rt.jar"
export NNTPSERVER="news.server.com"

for file in /etc/profile.d/*.sh ; do
  if [ -x $file ]; then
    . $file
fi
done

# For root users, ensure that /usr/local/sbin, /usr/sbin, and /sbin are in
# the $PATH.
if [ "`id -u`" = "0" ]; then
  echo $PATH | grep /usr/local/sbin 1> /dev/null 2> /dev/null
  if [ ! $? = 0 ]; then
    export PATH="/usr/local/sbin:/usr/sbin:/sbin:$PATH"
  fi
fi

# For non-root users, add the current directory to the search path:
if [ ! "`id u`" = "0" ]; then
 PATH="$PATH:."
fi

# Umask of 022
umask 022

# Options for zsh
export HISTFILE=~/.zsh_history
export HISTSIZE=50000
export SAVEHIST=50000
eval `dircolors -z`

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.

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

External Resources