[http://zsh.sourceforge.net/Intro/intro_1.html 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 {{ic|emulate sh}}), it offers advantages such as improved [http://zsh.sourceforge.net/Guide/zshguide06.html tab completion] and [http://zsh.sourceforge.net/Doc/Release/Expansion.html globbing].

[http://zsh.sourceforge.net/Intro/intro_1.html 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 {{ic|emulate sh}}), it offers advantages such as improved [http://zsh.sourceforge.net/Guide/zshguide06.html tab completion] and [http://zsh.sourceforge.net/Doc/Release/Expansion.html globbing].

Line 45:

Line 46:

}}

}}

−

When starting Zsh, it'll source the following files in this order by default:

+

When starting Zsh, it will source the following files in this order by default:

−

;{{ic|/etc/zsh/zshenv}}:Used for setting system-wide [[environment variables]]; it should not contain commands that produce output or assume the shell is attached to a tty. This file will '''''always''''' be sourced, this cannot be overridden.

+

* {{ic|/etc/zsh/zshenv}} Used for setting system-wide [[environment variables]]; it should not contain commands that produce output or assume the shell is attached to a tty. This file will '''''always''''' be sourced, this cannot be overridden.

−

;{{ic|$ZDOTDIR/.zshenv}}:Used for setting user's environment variables; it should not contain commands that produce output or assume the shell is attached to a tty. This file will '''''always''''' be sourced.

+

* {{ic|$ZDOTDIR/.zshenv}} Used for setting user's environment variables; it should not contain commands that produce output or assume the shell is attached to a tty. This file will '''''always''''' be sourced.

−

;{{ic|/etc/zsh/zprofile}}:Used for executing commands at start, will be sourced when starting as a '''''login shell'''''. Please note that on Arch Linux, by default it contains [https://projects.archlinux.org/svntogit/packages.git/tree/trunk/zprofile?h=packages/zsh one line] which source the {{ic|/etc/profile}}.

+

* {{ic|/etc/zsh/zprofile}} Used for executing commands at start, will be sourced when starting as a '''''login shell'''''. Please note that on Arch Linux, by default it contains [https://projects.archlinux.org/svntogit/packages.git/tree/trunk/zprofile?h=packages/zsh one line] which source the {{ic|/etc/profile}}.

−

:;{{ic|/etc/profile}}:This file should be sourced by all Bourne-compatible shells upon login: it sets up {{ic|$PATH}} and other environment variables and application-specific ({{ic|/etc/profile.d/*.sh}}) settings upon login.

+

** {{ic|/etc/profile}} This file should be sourced by all Bourne-compatible shells upon login: it sets up {{ic|$PATH}} and other environment variables and application-specific ({{ic|/etc/profile.d/*.sh}}) settings upon login.

−

;{{ic|$ZDOTDIR/.zprofile}}:Used for executing user's commands at start, will be sourced when starting as a '''''login shell'''''.

+

* {{ic|$ZDOTDIR/.zprofile}} Used for executing user's commands at start, will be sourced when starting as a '''''login shell'''''.

−

;{{ic|/etc/zsh/zshrc}}:Used for setting interactive shell configuration and executing commands, will be sourced when starting as an '''''interactive shell'''''.

+

* {{ic|/etc/zsh/zshrc}} Used for setting interactive shell configuration and executing commands, will be sourced when starting as an '''''interactive shell'''''.

−

;{{ic|$ZDOTDIR/.zshrc}}:Used for setting user's interactive shell configuration and executing commands, will be sourced when starting as an '''''interactive shell'''''.

+

* {{ic|$ZDOTDIR/.zshrc}} Used for setting user's interactive shell configuration and executing commands, will be sourced when starting as an '''''interactive shell'''''.

−

;{{ic|/etc/zsh/zlogin}}:Used for executing commands at ending of initial progress, will be sourced when starting as a '''''login shell'''''.

+

* {{ic|/etc/zsh/zlogin}} Used for executing commands at ending of initial progress, will be sourced when starting as a '''''login shell'''''.

−

;{{ic|$ZDOTDIR/.zlogin}}:Used for executing user's commands at ending of initial progress, will be sourced when starting as a '''''login shell'''''.

+

* {{ic|$ZDOTDIR/.zlogin}} Used for executing user's commands at ending of initial progress, will be sourced when starting as a '''''login shell'''''.

−

;{{ic|$ZDOTDIR/.zlogout}}:Will be sourced when a '''''login shell''''' '''exits'''.

+

* {{ic|$ZDOTDIR/.zlogout}} Will be sourced when a '''''login shell''''' '''exits'''.

−

;{{ic|/etc/zsh/zlogout}}:Will be sourced when a '''''login shell''''' '''exits'''.

+

* {{ic|/etc/zsh/zlogout}} Will be sourced when a '''''login shell''''' '''exits'''.

{{Note|

{{Note|

* The paths used in Arch's {{Pkg|zsh}} package are different from the default ones used in the [[man page]]s ({{Bug|48992}}).

* The paths used in Arch's {{Pkg|zsh}} package are different from the default ones used in the [[man page]]s ({{Bug|48992}}).

−

* {{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|$ZDOTDIR/.zshenv}}. To prevent this, please set the {{ic|$PATH}} variable in {{ic|$ZDOTDIR/.zprofile}}.

+

* {{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|$ZDOTDIR/.zshenv}}. To prevent this, please [[#Configuring $PATH|set the {{ic|$PATH}} variable]] in {{ic|$ZDOTDIR/.zprofile}}.

}}

}}

−

{{Warning|It is not recommended to replace the default [https://projects.archlinux.org/svntogit/packages.git/tree/trunk/zprofile?h&#61;packages/zsh one line] in {{ic|/etc/zsh/zprofile}} with something other, it will break the integrality of other packages which provide some scripts in {{ic|/etc/profile.d}}.}}

+

{{Warning|It is not recommended to replace the default [https://projects.archlinux.org/svntogit/packages.git/tree/trunk/zprofile?h&#61;packages/zsh one line] in {{ic|/etc/zsh/zprofile}} with something other, it will break the integrality of other packages which provide some scripts in {{ic|/etc/profile.d/}}.}}

== Configure Zsh ==

== Configure Zsh ==

Line 139:

Line 140:

ZLE has an [[emacs]] mode and a [[vi]] mode. If one of the {{ic|$VISUAL}} or {{ic|$EDITOR}} environment variables contain the string {{ic|vi}} then vi mode will be used; otherwise, it will default to emacs mode. Set the mode explicitly with {{ic|bindkey -e}} or {{ic|bindkey -v}} respectively for emacs mode or vi mode.

ZLE has an [[emacs]] mode and a [[vi]] mode. If one of the {{ic|$VISUAL}} or {{ic|$EDITOR}} environment variables contain the string {{ic|vi}} then vi mode will be used; otherwise, it will default to emacs mode. Set the mode explicitly with {{ic|bindkey -e}} or {{ic|bindkey -v}} respectively for emacs mode or vi mode.

−

See [http://zshwiki.org/home/zle/bindkeys ZshWiki: bindkeys] for instructions on keybinding setup.

+

See [http://zshwiki.org/home/zle/bindkeys ZshWiki: zle:bindkeys] for instructions on keybinding setup.

=== History search ===

=== History search ===

Line 157:

Line 158:

=== Prompts ===

=== Prompts ===

+

+

{{Note|Zsh introduced a bug in 5.3 with multi-line prompts. Tab completion will move your cursor up deleting a line of your prompt. See [https://www.zsh.org/mla/workers//2017/msg00021.html] for more details. For temporary workarounds you can either use Zsh 5.2, or attempt a number of workarounds listed [https://github.com/sorin-ionescu/prezto/issues/1245].}}

==== Prompt themes ====

==== Prompt themes ====

Line 183:

Line 186:

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 {{ic|1=PROMPT=}} with prompt escapes.

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 {{ic|1=PROMPT=}} with prompt escapes.

−

See [http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html Prompt Expansion] for a list of prompt variables and conditional substrings, or take a look at the {{man|1|zshmisc|url=}} manpage.

+

See [http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html Prompt Expansion] for a list of prompt variables and conditional substrings, or take a look at the {{man|1|zshmisc}} manpage.

===== Colors =====

===== Colors =====

Line 189:

Line 192:

Zsh sets colors differently than [[Color_Bash_Prompt|Bash]].

Zsh sets colors differently than [[Color_Bash_Prompt|Bash]].

−

See [http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Visual-effects Visual effects] in {{man|1|zshmisc|url=}} for prompt escapes to set foreground color, background color and other visual effects.

+

See [http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Visual-effects Visual effects] in {{man|1|zshmisc}} for prompt escapes to set foreground color, background color and other visual effects.

−

Colors can be specified by [https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg numeric color code] or by name (see {{man|1|zshzle|url=}} section [http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Character-Highlighting CHARACTER HIGHLIGHTING]). Most terminals support the following colors by name:

+

Colors can be specified by [https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg numeric color code] or by name (see {{man|1|zshzle}} section [http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Character-Highlighting CHARACTER HIGHLIGHTING]). Most terminals support the following colors by name:

{| class="wikitable"

{| class="wikitable"

Line 308:

Line 311:

cdr allows you to change the working directory to a previous working directory from a list maintained automatically. It stores all entries in files that are maintained across sessions and (by default) between terminal emulators in the current session.

cdr allows you to change the working directory to a previous working directory from a list maintained automatically. It stores all entries in files that are maintained across sessions and (by default) between terminal emulators in the current session.

Revision as of 10:55, 1 October 2017

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 advantages such as improved tab completion and globbing.

Startup/Shutdown files

If option RCS is unset in any of the files, no configuration files will be sourced after that file.

If option GLOBAL_RCS is unset in any of the files, no global configuration files (/etc/zsh/*) will be sourced after that file.

When starting Zsh, it will source the following files in this order by default:

/etc/zsh/zshenv Used for setting system-wide environment variables; it should not contain commands that produce output or assume the shell is attached to a tty. This file will always be sourced, this cannot be overridden.

$ZDOTDIR/.zshenv Used for setting user's environment variables; it should not contain commands that produce output or assume the shell is attached to a tty. This file will always be sourced.

/etc/zsh/zprofile Used for executing commands at start, will be sourced when starting as a login shell. Please note that on Arch Linux, by default it contains one line which source the /etc/profile.

/etc/profile This file should be sourced by all Bourne-compatible shells upon login: it sets up $PATH and other environment variables and application-specific (/etc/profile.d/*.sh) settings upon login.

$ZDOTDIR/.zprofile Used for executing user's commands at start, will be sourced when starting as a login shell.

/etc/zsh/zshrc Used for setting interactive shell configuration and executing commands, will be sourced when starting as an interactive shell.

$ZDOTDIR/.zshrc Used for setting user's interactive shell configuration and executing commands, will be sourced when starting as an interactive shell.

/etc/zsh/zlogin Used for executing commands at ending of initial progress, will be sourced when starting as a login shell.

$ZDOTDIR/.zlogin Used for executing user's commands at ending of initial progress, will be sourced when starting as a login shell.

$ZDOTDIR/.zlogout Will be sourced when a login shellexits.

/etc/zsh/zlogout Will be sourced when a login shellexits.

Note:

The paths used in Arch's zsh package are different from the default ones used in the man pages (FS#48992).

/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 $ZDOTDIR/.zshenv. To prevent this, please set the $PATH variable in $ZDOTDIR/.zprofile.

Warning: It is not recommended to replace the default one line in /etc/zsh/zprofile with something other, it will break the integrality of other packages which provide some scripts in /etc/profile.d/.

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.

Tip: Apply the changes without needing to logout and then back in by running source ~/.zshrc.

Here is a simple .zshrc:

~/.zshrc

autoload -Uz compinit promptinit
compinit
promptinit
# This will set the default prompt to the walters theme
prompt walters

Configuring $PATH

Normally, the path should be set in ~/.zshenv, but Arch Linux sources /etc/profile after sourcing ~/.zshenv.

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 -Uz 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.

Warning: This makes the 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 ~/.ssh/known_hosts somewhere else so that ssh creates a new one with un-hashed hostnames (previously known hosts will thus be lost). For more information, see the SSH readme for hashed-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 COMPLETE_ALIASES

Key bindings

Zsh does not use readline, instead it uses its own and more powerful Zsh Line Editor, ZLE. It does not read /etc/inputrc or ~/.inputrc.
ZLE has an emacs mode and a vi mode. If one of the $VISUAL or $EDITOR environment variables contain the string vi then vi mode will be used; otherwise, it will default to emacs mode. Set the mode explicitly with bindkey -e or bindkey -v respectively for emacs mode or vi mode.

By doing this, only the past commands matching the current line up to the current cursor position will be shown when Up or Down keys are pressed.

Prompts

Note: Zsh introduced a bug in 5.3 with multi-line prompts. Tab completion will move your cursor up deleting a line of your prompt. See [1] for more details. For temporary workarounds you can either use Zsh 5.2, or attempt a number of workarounds listed [2].

Prompt themes

There is a quick and easy way to set up a colored prompt in Zsh. Make sure that prompt theme system is set to autoload in .zshrc. This can be done by adding these lines to:

~/.zshrc

autoload -Uz promptinit
promptinit

Available prompt themes are listed by running the command:

$ prompt -l

For example, to use the walters theme, enter:

$ prompt walters

To preview all available themes, use this command:

$ prompt -p

Customized 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 prompt escapes.

Note: Bold text does not necessarily use the same colors as normal text. For example, %F{yellow}text%f looks brown or a very dark yellow, while %F{yellow}%Btext%b%f looks like bright or regular yellow.

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.

The ttyctl command

[3] 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

Remembering recent directories

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 your configuration file:

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.

Note: This will not work if you have more than one zsh session open, and attempt to cd, due to a conflict in both sessions writing to the same file.

cdr

cdr allows you to change the working directory to a previous working directory from a list maintained automatically. It stores all entries in files that are maintained across sessions and (by default) between terminal emulators in the current session.

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:

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.