zsh Configuration and Installation Guide
To install zsh under Gentoo, we need to emerge the app-shells/zsh
(zsh itself) and app-shells/zsh-completion (zsh completion scripts)
Code Listing 1.1: Emerging zsh
# emerge zsh zsh-completion
When you start zsh for the first time, you get the following message.
You can skip this menu as we will create an init file later in this guide.
Code Listing 1.2: Skipping the initial config
This is the Z Shell configuration function for new users, zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~). This function can help you with a few settings that should
make your use of the shell easier.
(q) Quit and do nothing. The function will be run again next time.
(0) Exit, creating the file ~/.zshrc containing just a comment.
That will prevent this function being run again.
(1) Continue to the main menu.
--- Type one of the keys in parentheses --- q
To start zsh for a single session, run zsh. For a more permanent
solution, please refer to the chapter on Saving
settings. After the installation, zsh seems to be very simple and
rather limited in the area of functionality. To change this, several
configuration changes need to be performed.
To enable the famous zsh tab-completion system, you need to run the
Code Listing 2.1: Enabling completion
% autoload -U compinit
Default completion style is quite plain and ugly. If you want to improve its
appearance, enter the following commands:
Code Listing 2.2: Completion style improvements
% zstyle ':completion:*:descriptions' format '%U%B%d%b%u'
% zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b'
It is also a good idea to enable the auto-correction of the commands typed:
Code Listing 2.3: Enabling correction
% setopt correctall
The prompt may seem quite boring however, but we can fix it easily in several
steps. First, we need to initialize advanced prompt support:
Code Listing 2.4: Enabling advanced prompts
% autoload -U promptinit
Available prompts samples may be printed via the prompt -p command. Now
we can activate our favorite one:
Code Listing 2.5: Choosing a sample prompt
% prompt gentoo
If you're playing around with the different prompts, and you don't want to
see a sample of every one, you can use prompt -l to list the prompts
Another option is to create your own prompt. To do this you will need to set
the PS1 environment variable.
Code Listing 2.6: A simple custom prompt
% export PS1="[Test Prompt] > "
[Test Prompt] >
While it is good to be able to create a custom text prompt, zsh also provides
many escape sequences that allow system information to be inserted into the
prompt. Some of the escape sequences available are:
||System time (HH:MM)
||System time (HH:MM:SS)
||System date (YY-MM-DD)
|%B - %b
||Begin - end bold print
|%U - %u
||Begin - end underlining
||Your current working directory
||Your current working directory, relative to ~
||The computer's hostname
||The computer's hostname (truncated before the first period)
||Your current tty
These escape sequences may simply be inserted into the environment variable,
PS1, and zsh will parse them automatically.
Code Listing 2.7: A more complex prompt
% export PS1="[%* - %D] %d %% "
[08:44:23 - 06-02-18] /home/username %
Unfortunately, the default zsh configuration in Gentoo does not include command
history support. As working with a shell without history is very frustrating,
we should enter the following commands:
Code Listing 2.8: Basic history configuration
% export HISTSIZE=2000
% export HISTFILE="$HOME/.history"
% export SAVEHIST=$HISTSIZE
To prevent history from recording duplicated entries (such as ls -l
entered many times during single shell session), you can set the
Code Listing 2.9: Setting ignoring of duplicates
% setopt hist_ignore_all_dups
A useful trick to prevent particular entries from being recorded
into a history by preceding them with at least one space.
Code Listing 2.10: Preventing particular entry from being recorded
% setopt hist_ignore_space
% cat /proc/cpuinfo
You can set the autocd option if you want to avoid tedious typing of
cd command while changing current directory (for example
/etc instead of cd /etc).
Code Listing 2.11: Setting autocd option
% setopt autocd
If standard bash-like globbing does not satisfy you, extendedglob
option may be set to enable extended globbing (one similar to
Code Listing 2.12: Setting extended globbing
% setopt extendedglob
When option above is set, you are able to use extended globbing queries
such as cp ^*.(tar|bz2|gz).
Saving zsh settings
Once we have customized zsh the way we like it, it is a good idea to save
these options as the zsh defaults for the system. One possible way to
achieve this is to write our settings in the /etc/zsh/zshrc script.
Alternatively, we could make them the defaults for our account only be editing
Code Listing 3.1: An example zshrc
autoload -U compinit
autoload -U promptinit
Making zsh default shell
Option for users with root privileges
We can change shell for given user with usermod command.
Code Listing 4.1: Switching to zsh permanently using usermod
# usermod -s /bin/zsh userlogin
Alternative for non-root users
If your system administrator (despite gentle requests, that is) refuses to set
the shell to zsh for you, you can set bash to execute zsh on
startup, all you need to do is make a slight modification to your
Code Listing 4.2: Sample ~/.bashrc
Another method of changing shells is to use the chsh command (a utility
used to change a user's login shell). A normal user may only change the login
shell for his own account. As root, you can change the login shell of any user.
Code Listing 4.3: Switching to zsh permanently using chsh
$ chsh -s /bin/zsh username
The contents of this document, unless otherwise expressly stated, are licensed under the CC-BY-SA-2.5 license. The Gentoo Name and Logo Usage Guidelines apply.