Difference between revisions of "Zsh"

From ArchWiki
Jump to: navigation, search
m (Typo fix)
(46 intermediate revisions by 22 users not shown)
Line 1: Line 1:
[[Category:Command shells (English)]]
+
[[Category:Command shells]]
{{i18n|Zsh}}
+
[[cs:Zsh]]
 +
[[de:Zsh]]
 
[[fr:Zsh]]
 
[[fr:Zsh]]
 +
[[zh-CN:Zsh]]
 
[http://www.zsh.org 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:
 
[http://www.zsh.org 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:
  
Line 45: Line 47:
  
 
==Configuration files==
 
==Configuration files==
{{Accuracy}}
 
 
At login, Zsh sources the following files in this order:
 
At login, Zsh sources the following files in this order:
;{{ic|/etc/profile}}: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.
 
 
;{{ic|~/.zshenv}}: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.  
 
;{{ic|~/.zshenv}}: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.  
 +
;{{ic|/etc/profile}}:This file is sourced by all Bourne-compatible shells upon login: it sets up an environment upon login and application-specific ({{ic|/etc/profile.d/*.sh}}) settings.
 
;{{ic|~/.zprofile}}:This file is generally used for automatic execution of user's scripts.
 
;{{ic|~/.zprofile}}:This file is generally used for automatic execution of user's scripts.
 
;{{ic|~/.zshrc}}:This is Zsh's main configuration file.
 
;{{ic|~/.zshrc}}:This is Zsh's main configuration file.
Line 57: Line 58:
 
{{Note|
 
{{Note|
 
*The paths used in Arch's {{Pkg|zsh}} package are different from the default ones used in the man pages.
 
*The paths used in Arch's {{Pkg|zsh}} package are different from the default ones used in the man pages.
*{{Ic|$ZDOTDIR}} defaults to {{Ic|$HOME}}}}
+
*{{Ic|$ZDOTDIR}} defaults to {{Ic|$HOME}}
 +
*{{ic|/etc/profile}} is not a part of the regular list of startup files run for Zsh, but is sourced from {{ic|/etc/zsh/zprofile}} in the {{Pkg|zsh}} package. Users should take note that {{ic|/etc/profile}} sets the {{ic|$PATH}} variable which will overwrite any {{ic|$PATH}} variable set in {{ic|~/.zshenv}}. To prevent this, either replace the {{ic|/etc/zsh/zprofile}} file with your own, or set your {{ic|$PATH}} variable from {{ic|~/.zshrc}}.
 +
}}
  
 
==~/.zshrc configuration==
 
==~/.zshrc configuration==
Line 86: Line 89:
 
compinit}}
 
compinit}}
  
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 [http://itso.iu.edu/Hashing_the_OpenSSH_known__hosts_File "Island-hopping" attacks]). In that intention, comment the following line or set the value to "no":
+
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 {{ic|~/.ssh/known_hosts}}.
 +
{{Warning|This makes your computer vulnerable to [http://nms.lcs.mit.edu/projects/ssh/README.hashed-hosts "Island-hopping" attacks]. In that intention, comment the following line or set the value to {{ıc|no}}:
 
{{hc|/etc/ssh/ssh_config|
 
{{hc|/etc/ssh/ssh_config|
 
#HashKnownHosts yes}}
 
#HashKnownHosts yes}}
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).
+
And move your {{ic|~/.ssh/known_hosts}} somewhere else so that ssh creates a new one with un-hashed hostnames (previously known hosts will thus be lost).
 +
}}
  
 
For autocompletion with an arrow-key driven interface, add the following to:
 
For autocompletion with an arrow-key driven interface, add the following to:
 
{{hc|~/.zshrc|
 
{{hc|~/.zshrc|
 
zstyle ':completion:*' menu select}}
 
zstyle ':completion:*' menu select}}
 +
:''To activate the menu, press tab twice.''
  
 
For autocompletion of command line switches for aliases, add the following to:
 
For autocompletion of command line switches for aliases, add the following to:
Line 101: Line 107:
 
=== Key Bindings ===
 
=== Key Bindings ===
 
Zsh does not use readline, instead it uses its own and more powerful zle. It does not read {{ic|/etc/inputrc}} or {{ic|~/.inputrc}}.
 
Zsh does not use readline, instead it uses its own and more powerful zle. It does not read {{ic|/etc/inputrc}} or {{ic|~/.inputrc}}.
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 is empty, it will default to [[emacs]]. You can change this with bindkey -v or bindkey -e.
+
zle has an [[emacs]] mode and a [[vi]] mode. By default, it tries to guess whether you want emacs or vi keys from the {{ic|$EDITOR}} environment variable. If it is empty, it will default to emacs. You can change this with {{ic|bindkey -v}} or {{ic|bindkey -e}}.
  
 
To get some special keys working:
 
To get some special keys working:
{{hc|~/.zshrc|
+
{{hc|~/.zshrc|2=
bindkey "\e[1~" beginning-of-line # Home
+
# create a zkbd compatible hash;
bindkey "\e[4~" end-of-line # End
+
# to add other keys to this hash, see: man 5 terminfo
bindkey "\e[5~" beginning-of-history # PageUp
+
typeset -A key
bindkey "\e[6~" end-of-history # PageDown
+
bindkey "\e[2~" quoted-insert # Ins
+
bindkey "\e[3~" delete-char # Del
+
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 "\e[Z" reverse-menu-complete # Shift+Tab
+
# for rxvt
+
bindkey "\e[7~" beginning-of-line # Home
+
bindkey "\e[8~" end-of-line # End
+
# 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
+
# for guake
+
bindkey "\eOF" end-of-line
+
bindkey "\eOH" beginning-of-line
+
bindkey "<nowiki>^[[1;5D</nowiki>" backward-word
+
bindkey "<nowiki>^[[1;5C</nowiki>" forward-word
+
bindkey "\e[3~" delete-char # Del
+
}}
+
  
Alternatively, you can convert /etc/inputrc for Zsh:
+
key[Home]=${terminfo[khome]}
{{hc|~/.zshrc|
+
 
# bind special keys according to readline configuration
+
key[End]=${terminfo[kend]}
eval "$(sed -n 's/^/bindkey /; s/: / /p' /etc/inputrc)"
+
key[Insert]=${terminfo[kich1]}
 +
key[Delete]=${terminfo[kdch1]}
 +
key[Up]=${terminfo[kcuu1]}
 +
key[Down]=${terminfo[kcud1]}
 +
key[Left]=${terminfo[kcub1]}
 +
key[Right]=${terminfo[kcuf1]}
 +
key[PageUp]=${terminfo[kpp]}
 +
key[PageDown]=${terminfo[knp]}
 +
 
 +
# setup key accordingly
 +
[[ -n "${key[Home]}"    ]]  && bindkey  "${key[Home]}"    beginning-of-line
 +
[[ -n "${key[End]}"    ]]  && bindkey "${key[End]}"    end-of-line
 +
[[ -n "${key[Insert]}"  ]]  && bindkey  "${key[Insert]}"  overwrite-mode
 +
[[ -n "${key[Delete]}"  ]]  && bindkey  "${key[Delete]}"  delete-char
 +
[[ -n "${key[Up]}"      ]]  && bindkey  "${key[Up]}"      up-line-or-history
 +
[[ -n "${key[Down]}"    ]]  && bindkey  "${key[Down]}"    down-line-or-history
 +
[[ -n "${key[Left]}"    ]]  && bindkey  "${key[Left]}"    backward-char
 +
[[ -n "${key[Right]}"  ]]  && bindkey  "${key[Right]}"  forward-char
 +
 
 +
# Finally, make sure the terminal is in application mode, when zle is
 +
# active. Only then are the values from $terminfo valid.
 +
 
 +
function zle-line-init () {
 +
    echoti smkx
 +
}
 +
function zle-line-finish () {
 +
    echoti rmkx
 +
}
 +
 
 +
zle -N zle-line-init
 +
zle -N zle-line-finish
 
}}
 
}}
  
{{Note|To get the proper sequences for certain key combinations, start {{Ic|cat}} or {{Ic|read}} without any parameters and press them; they should then be printed in the terminal. Both can be closed again via {{Keypress|Ctrl}}+{{Keypress|C}}.}}
+
{{Note|To get the proper sequences for certain key combinations, start {{Ic|cat}} or {{Ic|read}} without any parameters and press them; they should then be printed in the terminal. Both can be closed again via {{Keypress|Ctrl+C}}.}}
  
 
===History search===
 
===History search===
Line 147: Line 157:
  
 
{{hc|~/.zshrc|<nowiki>
 
{{hc|~/.zshrc|<nowiki>
bindkey "^[[A" history-beginning-search-backward
+
[[ -n "${key[PageUp]}  ]]  && bindkey "${key[PageUp]}"   history-beginning-search-backward
bindkey "^[[B" history-beginning-search-forward
+
[[ -n "${key[PageDown]} ]]  && bindkey  "${key[PageDown]}" history-beginning-search-forward
 
</nowiki>}}
 
</nowiki>}}
  
Line 155: Line 165:
 
===Prompts===
 
===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 {{ic|.zshrc}}. This can be done by adding these lines to:
+
There is a quick and easy way to set up a colored prompt in Zsh. Make sure that prompt is set to autoload in your {{ic|.zshrc}}. This can be done by adding these lines to:
  
 
{{hc|~/.zshrc|
 
{{hc|~/.zshrc|
Line 166: Line 176:
 
  $ prompt -l
 
  $ 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:
+
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 {{ic|walters}} prompt, you would enter:
 
   
 
   
 
  $ prompt walters
 
  $ prompt walters
Line 172: Line 182:
 
===Customizing your prompt===
 
===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:
+
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. You can customize it by using {{ic|1=PROMPT=}} with the following variables:
  
 
====Prompt variables====
 
====Prompt variables====
Line 189: Line 199:
  
 
=====Directories=====
 
=====Directories=====
; %~ : The current working directory. If you are in you are in your $HOME, this will be replaced by "~".
+
; %~ : The current working directory. If you are in you are in your {{ic|$HOME}}, this will be replaced by {{ic|~}}.
 
; %d : The current working directory.
 
; %d : The current working directory.
  
Line 199: Line 209:
 
; %B [...] %b : Begin and end bold print
 
; %B [...] %b : Begin and end bold print
 
; %{ [...] %} : Begin and enter area that will not be printed. Useful for setting colors.
 
; %{ [...] %} : Begin and enter area that will not be printed. Useful for setting colors.
 +
:In fact, this tag forces Zsh to ignore anything inside them when making indents for the prompt as well.
 +
:As such, not to use it can have some weird effects on the margins and indentation of the prompt.
  
 
=====Colors=====
 
=====Colors=====
Line 205: Line 217:
  
 
Following commands would now produce the color escape sequence needed to set the requested color when the prompt is printed:
 
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)
+
; $fg[color] : will set the text color (red, green, blue, etc. - defaults to bold)
; $reset_color : will reset the textcolor to white
+
; $fg_no_bold[color]: will set the non-bold text color
It is useful to put these commands in {{Ic|%{ [...] %}}}.
+
; $fg_bold[color]: will set the bold text color
 +
; $reset_color : will reset the text color to white
 +
It is useful to put these color commands inside {{Ic|%{ [...] %} }}, so the shell knows there is no output from these sequences and the cursor hasn't moved.
 +
 
 +
;Possible color values
 +
{| border="1"
 +
|-
 +
|black || red
 +
|-
 +
|green || yellow
 +
|-
 +
|blue || magenta
 +
|-
 +
|cyan || white
 +
|-
 +
|}
 +
Note that bold text doesn't necessarily use the same colors as normal text. For example, $fg['yellow'] looks brown or a very dark yellow, while $fg_no_bold['yellow'] looks like bright or regular yellow.
  
 
====Example====
 
====Example====
 
To have a two-sided prompt you could write:
 
To have a two-sided prompt you could write:
  PROMPT="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg[yellow]%}%1~ %{$reset_color%}%#"
+
  PROMPT="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg_no_bold[yellow]%}%1~ %{$reset_color%}%#"
  RPROMPT="[%{$fg[yellow]%}%?%{$reset_color%}]"
+
  RPROMPT="[%{$fg_no_bold[yellow]%}%?%{$reset_color%}]"
  
 
It would equal(without colors):
 
It would equal(without colors):
 
  username@host ~ %                                                        [0]
 
  username@host ~ %                                                        [0]
 
===Advanced .zshrc files===
 
 
One could also find a great way to manage its Zsh configuration by using [https://github.com/robbyrussell/oh-my-zsh Oh-my-zsh] created by Robby Russel which contains a lot of user contributed plugins and themes but most importantly a great community of more than 900 forks on github.
 
 
This is an example of a more advanced {{ic|.zshrc}}.
 
 
{{hc|~/.zshrc|2=<nowiki>
 
###########################################################       
 
# 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="nano"
 
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
 
 
# useful for path editing — backward-delete-word, but with / as additional delimiter
 
backward-delete-to-slash () {
 
  local WORDCHARS=${WORDCHARS//\//}
 
  zle .backward-delete-word
 
}
 
zle -N backward-delete-to-slash
 
 
##################################################################
 
# 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
 
bindkey '^[w' backward-delete-to-slash
 
# completion in the middle of a line
 
bindkey '^i' expand-or-complete-prefix
 
 
##################################################################
 
# 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 &'
 
</nowiki>}}
 
 
There are many more ways that you can customise Zsh, obviously far too many to list here, see the [http://zsh.sourceforge.net/Doc/Release/zsh.html Zsh manual] for more information.
 
  
 
===Sample .zshrc files===
 
===Sample .zshrc files===
Line 350: Line 249:
 
Here is a list of {{ic|.zshrc}} files. Feel free to add your own:
 
Here is a list of {{ic|.zshrc}} files. Feel free to add your own:
  
* [https://github.com/robbyrussell/oh-my-zsh Oh-my-zsh Plugin and Theme system for Zsh] is a must have to manage your zshrc file and to take advantage of a huge community of over 1400 forks on github;
+
* [https://github.com/robbyrussell/oh-my-zsh Oh-my-zsh Plugin and Theme system for Zsh] can help you manage your zshrc file and has a huge community of over 2000 forks on github;
 
* Basic setup, with dynamic prompt and window title/hardinfo => http://github.com/MrElendig/dotfiles-alice/blob/master/.zshrc;
 
* Basic setup, with dynamic prompt and window title/hardinfo => http://github.com/MrElendig/dotfiles-alice/blob/master/.zshrc;
 
* An Arch package named [https://www.archlinux.org/packages/extra/any/grml-zsh-config/ grml-zsh-config] comes from http://grml.org/zsh and provides a zshrc file that includes many tweaks for your zshell.
 
* An Arch package named [https://www.archlinux.org/packages/extra/any/grml-zsh-config/ grml-zsh-config] comes from http://grml.org/zsh and provides a zshrc file that includes many tweaks for your zshell.
Line 376: Line 275:
 
{{Warning| Failure to follow the above will result in all kinds of problems.}}
 
{{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:  
+
If you did not follow the above, you can still change the default shell back to Bash by editing {{ic|/etc/passwd}} as root.  
 +
 
 +
{{Warning| It is '''strongly''' recommended to use vipw when editing user information as it prevents badly formatted entries.}}
 +
 
 +
For example:  
 
   
 
   
 
from:
 
from:
Line 383: Line 286:
 
  username:x:1000:1000:Full Name,,,:/home/username:/bin/bash
 
  username:x:1000:1000:Full Name,,,:/home/username:/bin/bash
  
==External Resources==
+
==See also==
 
*[http://zsh.sourceforge.net/Intro/intro_1.html#SEC1 Zsh Introduction]
 
*[http://zsh.sourceforge.net/Intro/intro_1.html#SEC1 Zsh Introduction]
 
*[http://zsh.sourceforge.net/Guide/zshguide.html Users Guide]
 
*[http://zsh.sourceforge.net/Guide/zshguide.html Users Guide]
Line 392: Line 295:
 
*[http://www.bash2zsh.com/zsh_refcard/refcard.pdf Bash2Zsh Reference Card]
 
*[http://www.bash2zsh.com/zsh_refcard/refcard.pdf Bash2Zsh Reference Card]
 
*[https://github.com/robbyrussell/oh-my-zsh Oh My Zshell by Robby Russell]  
 
*[https://github.com/robbyrussell/oh-my-zsh Oh My Zshell by Robby Russell]  
*[http://www.gentoo.org/doc/en/zsh.xml Gentoo zsh wiki article]
+
*[http://www.gentoo.org/doc/en/zsh.xml Gentoo Linux Documentation -- zsh Configuration and Installation Guide]
 
*[http://my.opera.com/blackbelt_jones/blog/2007/06/05/zsh-prompt-configuration-issue-solved Setting up the zsh prompt]
 
*[http://my.opera.com/blackbelt_jones/blog/2007/06/05/zsh-prompt-configuration-issue-solved Setting up the zsh prompt]
  
 
*'''IRC channel''': #zsh at irc.freenode.org
 
*'''IRC channel''': #zsh at irc.freenode.org

Revision as of 14:50, 1 February 2013

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 zsh package available in the official repositories.

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 /etc/shells you can use the chsh command to change your default shell without root access. If you installed Zsh from the official repositories, it should already have an entry in /etc/shells.

Change the default shell for the current user:

$ chsh -s $(which zsh)
Note: You have to log out 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 ~/.bashrc to ~/.zshrc (e.g. the prompt and the aliases) and from ~/.bash_profile to ~/.zprofile (e.g. the code that starts your X Window System).

Configuration files

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

~/.zshenv
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.
/etc/profile
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.
~/.zprofile
This file is generally used for automatic execution of user's scripts.
~/.zshrc
This is Zsh's main configuration file.
~/.zlogin
This file is generally used for automatic execution of user's scripts.

At logout it sources ~/.zlogout, which is used for automatic execution of user's scripts.

Note:
  • The paths used in Arch's zsh package are different from the default ones used in the man pages.
  • $ZDOTDIR defaults to $HOME
  • /etc/profile is not a part of the regular list of startup files run for Zsh, but is sourced from /etc/zsh/zprofile in the zsh package. Users should take note that /etc/profile sets the $PATH variable which will overwrite any $PATH variable set in ~/.zshenv. To prevent this, either replace the /etc/zsh/zprofile file with your own, or set your $PATH variable from ~/.zshrc.

~/.zshrc 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, configuring Zsh 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:

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

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:

~/.zshrc
autoload -U compinit
compinit

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: This makes your computer vulnerable to "Island-hopping" attacks. In that intention, comment the following line or set the value to no:
/etc/ssh/ssh_config
#HashKnownHosts yes

And move your ~/.ssh/known_hosts somewhere else so that ssh creates a new one with un-hashed hostnames (previously known hosts will thus be lost).

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

~/.zshrc
zstyle ':completion:*' menu select
To activate the menu, press tab twice.

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

~/.zshrc
setopt completealiases

Key Bindings

Zsh does not use readline, instead it uses its own and more powerful zle. It does not read /etc/inputrc or ~/.inputrc. 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 is empty, it will default to emacs. You can change this with bindkey -v or bindkey -e.

To get some special keys working:

~/.zshrc
# create a zkbd compatible hash;
# to add other keys to this hash, see: man 5 terminfo
typeset -A key

key[Home]=${terminfo[khome]}

key[End]=${terminfo[kend]}
key[Insert]=${terminfo[kich1]}
key[Delete]=${terminfo[kdch1]}
key[Up]=${terminfo[kcuu1]}
key[Down]=${terminfo[kcud1]}
key[Left]=${terminfo[kcub1]}
key[Right]=${terminfo[kcuf1]}
key[PageUp]=${terminfo[kpp]}
key[PageDown]=${terminfo[knp]}

# setup key accordingly
[[ -n "${key[Home]}"    ]]  && bindkey  "${key[Home]}"    beginning-of-line
[[ -n "${key[End]}"     ]]  && bindkey  "${key[End]}"     end-of-line
[[ -n "${key[Insert]}"  ]]  && bindkey  "${key[Insert]}"  overwrite-mode
[[ -n "${key[Delete]}"  ]]  && bindkey  "${key[Delete]}"  delete-char
[[ -n "${key[Up]}"      ]]  && bindkey  "${key[Up]}"      up-line-or-history
[[ -n "${key[Down]}"    ]]  && bindkey  "${key[Down]}"    down-line-or-history
[[ -n "${key[Left]}"    ]]  && bindkey  "${key[Left]}"    backward-char
[[ -n "${key[Right]}"   ]]  && bindkey  "${key[Right]}"   forward-char

# Finally, make sure the terminal is in application mode, when zle is
# active. Only then are the values from $terminfo valid.

function zle-line-init () {
    echoti smkx
}
function zle-line-finish () {
    echoti rmkx
}

zle -N zle-line-init
zle -N zle-line-finish
Note: To get the proper sequences for certain key combinations, start cat or read without any parameters and press them; they should then be printed in the terminal. Both can be closed again via Template:Keypress.

History search

You can add these lines to your .zshrc

~/.zshrc
[[ -n "${key[PageUp]}   ]]  && bindkey  "${key[PageUp]}"    history-beginning-search-backward
[[ -n "${key[PageDown]} ]]  && bindkey  "${key[PageDown]}"  history-beginning-search-forward

Doing this, only past commands beginning with the current input would have 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 autoload in your .zshrc. This can be done by adding these lines to:

~/.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

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. You can customize it by using PROMPT= with the following variables:

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.
In fact, this tag forces Zsh to ignore anything inside them when making indents for the prompt as well.
As such, not to use it can have some weird effects on the margins and indentation of the prompt.
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 text color (red, green, blue, etc. - defaults to bold)
$fg_no_bold[color]
will set the non-bold text color
$fg_bold[color]
will set the bold text color
$reset_color 
will reset the text color to white

It is useful to put these color commands inside %{ [...] %} , so the shell knows there is no output from these sequences and the cursor hasn't moved.

Possible color values
black red
green yellow
blue magenta
cyan white

Note that bold text doesn't necessarily use the same colors as normal text. For example, $fg['yellow'] looks brown or a very dark yellow, while $fg_no_bold['yellow'] looks like bright or regular yellow.

Example

To have a two-sided prompt you could write:

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

It would equal(without colors):

username@host ~ %                                                         [0]

Sample .zshrc files

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

Global configuration

Occasionally you might want to have some settings applied globally to all zsh users. The zsh wiki tells us that there are some global configuration files, for example /etc/zshrc. This however is slightly different on ArchLinux, since it has been compiled with flags specifically to target /etc/zsh/ instead.

So, for global configuration use /etc/zsh/zshrc, not /etc/zshrc. The same goes for /etc/zsh/zshenv, /etc/zsh/zlogin and /etc/zsh/zlogout. Note that these files are not installed by default, so you need to create them yourself if you want to use them.

The only exception is zprofile, use /etc/profile instead.

Autostarting applications

Zsh always executes /etc/zsh/zshenv and $ZDOTDIR/.zshenv so do not bloat these files.

If the shell is a login shell, commands are read from /etc/profile and then $ZDOTDIR/.zprofile. Then, if the shell is interactive, commands are read from /etc/zsh/zshrc and then $ZDOTDIR/.zshrc. Finally, if the shell is a login shell, /etc/zsh/zlogin and $ZDOTDIR/.zlogin are read.

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.

Warning: It is strongly recommended to use vipw when editing user information as it prevents badly formatted entries.

For example:

from:

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

to:

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

See also

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