This guide details the installation and configuration for zsh.
- 1 Installation
- 2 Configuration
- 3 Usage
- 4 See also
Emerging zsh under Gentoo (app-shells/zsh) will install it:
emerge --ask app-shells/zsh
It is also a good idea to install the zsh auto-completion scripts (app-shells/zsh-completions):
emerge --ask app-shells/zsh-completions
When starting zsh for the first time, the following message will appear. You can skip this menu as we will create an init file later in this guide.
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. You can: (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 enable the famous zsh tab-completion system, the following commands must be run:
autoload -U compinit
Default completion style is quite plain and ugly. If you want to improve its appearance, enter the following commands:
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:
The prompt may seem quite boring however, but we can fix it easily in several steps. First, we need to initialize advanced prompt support:
autoload -U promptinit
Available prompts samples may be printed via the prompt -p command. Now we can activate our favorite one:
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 available.
Another option is to create your own prompt. To do this you will need to set the PS1 environment variable.
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:
|%T||System time (HH:MM)|
|%*||System time (HH:MM:SS)|
|%D||System date (YY-MM-DD)|
|%B - %b||Begin - end bold print|
|%U - %u||Begin - end underlining|
|%d||Your current working directory|
|%~||Your current working directory, relative to ~|
|%M||The computer's hostname|
|%m||The computer's hostname (truncated before the first period)|
|%l||Your current tty|
These escape sequences may simply be inserted into the environment variable, PS1, and zsh will parse them automatically.
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:
History won't be saved without the following command:
To prevent history from recording duplicated entries (such as ls -l entered many times during single shell session), you can set the
A useful trick to prevent particular entries from being recorded into a history by preceding them with at least one space.
The following command won't be recorded. Note the extra space before it:
You can set the
autocd option to avoid tedious typing of cd command while changing current directory (for example /etc instead of cd /etc).
If standard bash-like globbing does not satisfy you,
extendedglob option may be set to enable extended globbing (one similar to regular expressions).
When option above is set, you are able to use extended globbing queries such as cp ^*.(tar.
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 ~/.zshrc.
#!/bin/zsh # completion autoload -U compinit compinit # correction setopt correctall # prompt autoload -U promptinit promptinit prompt gentoo
Making zsh default shell
The regular way
The shell for the current user can be changed using the chsh command:
chsh -s /bin/zsh
This will set zsh as default shell for the current account. In order to set it as a shell for another account chsh must be ran with as root (super user) privileges.
Alternative (without chsh)
If a system administrator (despite gentle requests, that is) refuses to set the shell to zsh for you and chsh does not works for you, you can set bash to execute zsh on startup, all you need to do is make a slight modification to your ~/.bashrc.
## (Add the following to your ~/.bashrc) exec zsh
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.
chsh -s /bin/zsh username
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.
- Shell - The a meta-article listing the main repositories available shell software.
- bash - The Bourne Again Shell. The most popular POXIX-compliant shell on Linux systems, and the default shell on Gentoo.
- dash - The Debian Almquist Shell, a small, fast, and POSIX-compliant shell suited for startup scripts (as /bin/sh replacement).
This article is based on a document formerly found on our main website gentoo.org.
The following people contributed to the original document: Henryk Konsek, Andrew Morritt, Chris White, and Peter Weller
They are listed here as the Wiki history does not allow for any external attribution. If you edit the Wiki article, please do not add yourself here; your contributions are recorded on the history page.