Zsh
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 issuing emulate sh
), it offers many advantages such as:
- Efficiency
- Improved tab completion
- Improved globbing
- Improved array handling
- Fully customisable
The Zsh FAQ offers more reasons to use Zsh.
Contents
- 1 Installation
- 2 Configuration files
-
3 Configure Zsh
- 3.1 Simple .zshrc
- 3.2 Configuring $PATH
- 3.3 Command completion
- 3.4 The "command not found" hook
- 3.5 Preventing duplicate lines in the history
- 3.6 The ttyctl command
- 3.7 Key bindings
- 3.8 History search
- 3.9 Prompts
- 3.10 Customizing the prompt
- 3.11 Dirstack
- 3.12 Help command
- 3.13 Fish-like syntax highlighting
- 3.14 Sample .zshrc files
- 3.15 Configuration Frameworks
- 3.16 Autostarting applications
- 3.17 Persistent rehash
- 4 Uninstallation
- 5 See also
Installation
Before starting users may want to see what shell is currently being used:
$ echo $SHELL
Install the zsh package. For additional completion definitions, install the zsh-completions package as well.
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
. If you did not see it, you can invoke it manually with
$ zsh /usr/share/zsh/functions/Newuser/zsh-newuser-install -f
Making Zsh your default shell
See Command-line shell#Changing your default shell.
Configuration files
When starting Zsh, it'll source the following files in this order by default:
/etc/zsh/zshenv
- This file should contain commands to set the global command search path and other system-wide environment variables; it should not contain commands that produce output or assume the shell is attached to a tty.
~/.zshenv
- Similar to
/etc/zsh/zshenv
but for per-user configuration. Generally used for setting some useful environment variables. /etc/zsh/zprofile
- This is a global configuration file, it'll be sourced at login. Usually used for executing some general commands at login. Please note that on Arch Linux, by default it contains one line which source the
/etc/profile
, see below for details. /etc/profile
- This file should be sourced by all Bourne-compatible shells upon login: it sets up an environment upon login and application-specific (
/etc/profile.d/*.sh
) settings. Note that on Arch Linux, Zsh will also source this by default. ~/.zprofile
- This file is generally used for automatic execution of user's scripts at login.
/etc/zsh/zshrc
- Global configuration file, will be sourced when starting as a interactive shell.
~/.zshrc
- Main user configuration file, will be sourced when starting as a interactive shell.
/etc/zsh/zlogin
- A global configuration file, will be sourced at the ending of initial progress when starting as a login shell.
~/.zlogin
- Same as
/etc/zsh/zlogin
but for per-user configuration. /etc/zsh/zlogout
- A global configuration file, will be sourced when a login shell exits.
~/.zlogout
- Same as
/etc/zsh/zlogout
but for per-user configuration.
Global configuration files
Occasionally users might want to have some settings applied globally to all Zsh users. The zsh(1) said that there are some global configuration files, for example /etc/zshrc
. This however is slightly different on Arch, since it has been compiled with flags specifically to target[1] /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 create them if desired.
The only exception is zprofile, use /etc/profile
instead.
Configure Zsh
Although Zsh is usable out of the box, it is almost certainly not set up the way most users would like to use it, but due to the sheer amount of customization available in Zsh, configuring Zsh can be a daunting and time-consuming experience.
Simple .zshrc
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 customized. In order to use this configuration save it as a file named .zshrc
. Apply the changes without needing to logout and then back in by running:
$ source ~/.zshrc
Here is a simple .zshrc
:
~/.zshrc
autoload -U compinit promptinit compinit promptinit # This will set the default prompt to the walters theme prompt walters
Configuring $PATH
Information about setting up the system path per user in zsh can be found here: http://zsh.sourceforge.net/Guide/zshguide02.html#l24
In short, put the following in ~/.zshenv
:
~/.zshenv
typeset -U path path=(~/bin /other/things/in/path $path)
See also the note in #Configuration files.
Command completion
Perhaps the most compelling feature of Zsh is its advanced autocompletion abilities. At the very least, enable autocompletion in .zshrc
. To enable autocompletion, add the following to your ~/.zshrc
:
~/.zshrc
autoload -U compinit compinit
The above configuration includes ssh/scp/sftp hostnames completion but in order for this feature to work, users need to prevent ssh from hashing hosts names in ~/.ssh/known_hosts
.
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
The "command not found" hook
See Pkgfile#Command not found.
Preventing duplicate lines in the history
To ignore duplicate lines in the history, use the following:
~/.zshrc
setopt HIST_IGNORE_DUPS
To free the history from already created duplicates, run:
$ awk -i inplace '!x[$0]++' ~/.zsh_history
The ttyctl command
[2] describes the ttyctl
command in Zsh.
This may be used to "freeze/unfreeze" the terminal.
Many programs change the terminal state, and often do not restore terminal settings on exiting abnormally.
To avoid the need to manually reset the terminal, use the following:
~/.zshrc
ttyctl -f
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 emacs or vi keys from the $EDITOR
environment variable are desired. If it is empty, it will default to emacs. Change this with bindkey -e
or bindkey -v
respectively for emacs mode or vi mode.
See also zshwiki: bindkeys.
Bind key to ncurses application
Bind a ncurses application to a keystoke, but it will not accept interaction. Use BUFFER
variable to make it work. The following example lets users open ncmpcpp using Alt+\
:
~/.zshrc
ncmpcppShow() { BUFFER="ncmpcpp"; zle accept-line; } zle -N ncmpcppShow bindkey '^[\' ncmpcppShow
Alternate way to bind ncurses application
This method will keep everything you entered in the line before calling application
~/.zshrc
ncmpcppShow() { ncmpcpp <$TTY; zle redisplay; } zle -N ncmpcppShow bindkey '^[\' ncmpcppShow
File manager key binds
Key binds like those used in graphic file managers may come handy. The first comes back in directory history (Alt+Left
), the second let the user go to the parent directory (Alt+Up
). They also display the directory content.
~/.zshrc
cdUndoKey() { popd > /dev/null zle reset-prompt echo ls echo } cdParentKey() { pushd .. > /dev/null zle reset-prompt echo ls echo } zle -N cdParentKey zle -N cdUndoKey bindkey '^[[1;3A' cdParentKey bindkey '^[[1;3D' cdUndoKey
History search
Add these lines to .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 .zshrc
. This can be done by adding these lines to:
~/.zshrc
autoload -U promptinit promptinit
Available prompts are listed by running the command:
$ prompt -l
For example, to use the prompt walters
, enter:
$ prompt walters
To preview all available themes, use this command:
$ prompt -p
Customizing the prompt
For users who are dissatisfied with the prompts mentioned above (or want to expand their usefulness), Zsh offers the possibility to build a custom prompt. Zsh supports a left- and right-sided prompt additional to the single, left-sided prompt that is common to all shells. Customize it by using PROMPT=
with the following variables:
See Prompt Expansion for a list of prompt variables and conditional substrings.
Colors
Zsh sets colors differently than Bash. Add autoload -U colors && colors
before PROMPT=
in .zshrc
to use them. Usually you will want to put these inside %{ [...] %}
so the cursor does not move.
$fg[color]
will set the text color (red, green, blue, etc. - defaults to whatever format set prior to text)
Command | Description | |
---|---|---|
%F{color} [...] %f |
effectively the same as the previous, but with less typing. Can also prefix F with a number instead. | |
$fg_no_bold[color]
|
will set text to non-bold and set the text color | |
$fg_bold[color]
|
will set the text to bold and set the text color | |
$reset_color
|
will reset the text color to the default color. Does not reset bold. use %b to reset bold. Saves typing if it's just %f though. |
|
%K{color} [...] %k |
will set the background color. Same color as non-bold text color. Prefixing with any single-digit number makes the bg black. |
Possible color values | |
---|---|
black or 0 |
red or 1
|
green or 2 |
yellow or 3
|
blue or 4 |
magenta or 5
|
cyan or 6 |
white or 7
|
Example
This is an example of a two-sided prompt:
PROMPT="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m %{$fg_no_bold[yellow]%}%1~ %{$reset_color%}%#" RPROMPT="[%{$fg_no_bold[yellow]%}%?%{$reset_color%}]"
And here's how it will be displayed:
username@host ~ % [0]
Dirstack
Zsh can be configured to remember the DIRSTACKSIZE last visited folders. This can then be used to cd them very quickly. You need to add some lines to you configuration file:
.zshrc
DIRSTACKFILE="$HOME/.cache/zsh/dirs" if [[ -f $DIRSTACKFILE ]] && [[ $#dirstack -eq 0 ]]; then dirstack=( ${(f)"$(< $DIRSTACKFILE)"} ) [[ -d $dirstack[1] ]] && cd $dirstack[1] fi chpwd() { print -l $PWD ${(u)dirstack} >$DIRSTACKFILE } DIRSTACKSIZE=20 setopt autopushd pushdsilent pushdtohome ## Remove duplicate entries setopt pushdignoredups ## This reverts the +/- operators. setopt pushdminus
Now use
dirs -v
to print the dirstack. Use cd -<NUM>
to go back to a visited folder. Use autocompletion after the dash. This proves very handy if using the autocompletion menu.
Help command
Unlike bash, zsh does not enable a built in help
command. To use help
in zsh, add following to your zshrc
:
autoload -U run-help autoload run-help-git autoload run-help-svn autoload run-help-svk unalias run-help alias help=run-help
Fish-like syntax highlighting
Fish provides a very powerful shell syntax highlighting. To use this in zsh, you can install zsh-syntax-highlighting from offical repository and add following to your zshrc:
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
Sample .zshrc files
- A package in offical repository named grml-zsh-config comes from http://grml.org/zsh and provides a zshrc file that includes many tweaks for Zshell. This is the default configuration for the monthly ISO releases.
- Basic setup, with dynamic prompt and window title/hardinfo => http://github.com/MrElendig/dotfiles-alice/blob/master/.zshrc;
- https://github.com/slashbeast/things/blob/master/configs/DOTzshrc - zshrc with multiple features, be sure to check out comments into it. Notable features: confirm function to ensure that user want to run poweroff, reboot or hibernate, support for GIT in prompt (done without vcsinfo), tab completion with menu, printing current executed command into window's title bar and more.
Configuration Frameworks
- oh-my-zsh is a popular, community-driven framework for managing your Zsh configuration. It comes bundled with a ton of helpful functions, helpers, plugins, themes.
- Prezto - Instantly Awesome Zsh (available in AUR as prezto-gitAUR) is a configuration framework for Zsh. It comes with modules, enriching the command line interface environment with sane defaults, aliases, functions, auto completion, and prompt themes.
- Antigen (available in AUR as antigen-gitAUR) - A plugin manager for zsh, inspired by oh-my-zsh and vundle.
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.
See also the STARTUP/SHUTDOWN FILES section of man zsh
.
Persistent rehash
Typically, compinit will not automatically find new executables in the $PATH. For example, after you install a new package, the files in /usr/bin would not be immediately or automatically included in the completion. Thus, to have these new exectuables included, one would run:
$ rehash
This 'rehash' can be set to happen automatically. Simply include the following in your zshrc
:
~/.zshrc
zstyle ':completion:*' rehash true
Uninstallation
Change the default shell before removing the zsh package.
Run following command:
$ chsh -s /bin/bash user
Use it for every user with zsh set as their login shell (including root if needed). When completed, the zsh package can be removed.
Alternatively, change the default shell back to Bash by editing /etc/passwd
as root.
For example, change the following:
username:x:1000:1000:Full Name,,,:/home/username:/bin/zsh
To this:
username:x:1000:1000:Full Name,,,:/home/username:/bin/bash
See also
- A User's Guide to ZSH
- The Z Shell Manual (different format available here)
- Zsh FAQ
- zsh-lovers(1) (this is also available as zsh-lovers in offical repository)
- Zsh Wiki
- Gentoo Wiki: Zsh/HOWTO
- Bash2Zsh Reference Card