Difference between revisions of "Zsh"
(→Alternative method without using terminfo) |
m (→Sample .zshrc files: Improve syntax) |
||
(247 intermediate revisions by 69 users not shown) | |||
Line 2: | Line 2: | ||
[[cs:Zsh]] | [[cs:Zsh]] | ||
[[de:Zsh]] | [[de:Zsh]] | ||
+ | [[es:Zsh]] | ||
+ | [[fa:Zsh]] | ||
[[fr:Zsh]] | [[fr:Zsh]] | ||
− | [[zh- | + | [[ja:Zsh]] |
− | [http:// | + | [[ru:Zsh]] |
+ | [[zh-hans:Zsh]] | ||
+ | [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]. | ||
− | + | The [http://zsh.sourceforge.net/FAQ/zshfaq01.html#l4 Zsh FAQ] offers more reasons to use Zsh. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | The | ||
== Installation == | == Installation == | ||
− | Before starting | + | Before starting users may want to see what shell is currently being used: |
$ echo $SHELL | $ echo $SHELL | ||
− | [[ | + | [[Install]] the {{Pkg|zsh}} package. For additional completion definitions, install the {{pkg|zsh-completions}} package as well. |
=== Initial configuration === | === Initial configuration === | ||
Line 28: | Line 26: | ||
$ zsh | $ zsh | ||
− | You should now see | + | You should now see ''zsh-newuser-install'', which will walk you through some basic configuration. If you want to skip this, press {{ic|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 === | === Making Zsh your default shell === | ||
− | + | See [[Command-line shell#Changing your default shell]]. | |
− | + | {{Tip|If replacing {{Pkg|bash}}, users may want to move some code from {{ic|~/.bashrc}} to {{ic|~/.zshrc}} (e.g. the prompt and the [[Bash#Aliases|aliases]]) and from {{ic|~/.bash_profile}} to {{ic|~/.zprofile}} (e.g. [[xinit#Autostart X at login|the code that starts the X Window System]]).}} | |
− | + | == Startup/Shutdown files == | |
− | {{ | + | {{Tip|See [http://zsh.sourceforge.net/Guide/zshguide02.html A User's Guide to the Z-Shell] for explanation on interactive and login shells, and what to put in your startup files.}} |
− | + | {{Note| | |
− | + | * If {{ic|$ZDOTDIR}} is not set, {{ic|$HOME}} is used instead. | |
− | + | * If option {{ic|RCS}} is unset in any of the files, no configuration files will be sourced after that file. | |
+ | * If option {{ic|GLOBAL_RCS}} is unset in any of the files, no global configuration files ({{ic|/etc/zsh/*}}) will be sourced after that file. | ||
+ | }} | ||
− | + | 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|$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/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|/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|/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/.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 | + | * 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 [[#Configuring $PATH|set the $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| | ||
}} | }} | ||
− | + | {{Warning|It is not recommended to replace the default [https://projects.archlinux.org/svntogit/packages.git/tree/trunk/zprofile?h=packages/zsh one line] in {{ic|/etc/zsh/zprofile}} with something other, it will break the integrity of other packages which provide some scripts in {{ic|/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 {{ic|.zshrc}}. | |
+ | |||
+ | {{Tip|Apply the changes without needing to logout and then back in by running {{ic|source ~/.zshrc}}.}} | ||
− | Here is a simple {{ic|.zshrc}} | + | Here is a simple {{ic|.zshrc}}: |
{{hc|~/.zshrc| | {{hc|~/.zshrc| | ||
− | autoload - | + | autoload -Uz compinit promptinit |
compinit | compinit | ||
promptinit | promptinit | ||
# This will set the default prompt to the walters theme | # This will set the default prompt to the walters theme | ||
− | prompt walters}} | + | prompt walters |
+ | }} | ||
=== Configuring $PATH === | === Configuring $PATH === | ||
− | + | {{Out of date|{{ic|/etc/profile}} does not overwrite {{ic|$PATH}} anymore, it appends Arch default path to {{ic|$PATH}}.}} | |
− | + | Normally, the path should be set in {{ic|~/.zshenv}}, but Arch Linux sources {{ic|/etc/profile}} after sourcing {{ic|~/.zshenv}}. | |
− | {{hc|~/. | + | |
+ | To prevent your {{ic|$PATH}} being overwritten, set it in {{ic|~/.zprofile}}. | ||
+ | |||
+ | {{hc|~/.zprofile|2= | ||
typeset -U path | typeset -U path | ||
− | path | + | path=(''~/.local/bin'' ''/other/things/in/path'' $path[@]) |
+ | }} | ||
+ | |||
+ | See also [http://zsh.sourceforge.net/Guide/zshguide02.html#l24 A User's Guide to the Z-Shell] and the note in [[#Startup/Shutdown files]]. | ||
=== Command completion === | === Command completion === | ||
− | Perhaps the most compelling feature of Zsh is its advanced autocompletion abilities. At the very least, | + | Perhaps the most compelling feature of Zsh is its advanced autocompletion abilities. At the very least, enable autocompletion in {{ic|.zshrc}}. To enable autocompletion, add the following to your {{ic|~/.zshrc}}: |
{{hc|~/.zshrc| | {{hc|~/.zshrc| | ||
− | autoload - | + | autoload -Uz compinit |
− | compinit}} | + | compinit |
+ | }} | ||
− | The above configuration includes ssh/scp/sftp hostnames completion but in order for this feature to work | + | The above configuration includes ssh/scp/sftp hostnames completion but in order for this feature to work, users must not enable ssh's hostname hashing (i.e. option {{ic|HashKnownHosts}} in ssh client configuration). |
− | {{ | ||
− | |||
− | |||
− | |||
− | |||
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 {{ic|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: | ||
+ | |||
{{hc|~/.zshrc| | {{hc|~/.zshrc| | ||
− | setopt | + | 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 {{ic|/etc/inputrc}} or {{ic|~/.inputrc}}. | |
+ | |||
+ | 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: zle:bindkeys] for instructions on keybinding setup. | |
− | + | === History search === | |
− | |||
− | |||
− | |||
− | + | You need to set up [[#Key bindings]] to use this. To enable history search add these lines to {{ic|.zshrc}} file: | |
− | {{hc|~/.zshrc| | + | {{hc|~/.zshrc|<nowiki> |
− | + | autoload -Uz up-line-or-beginning-search down-line-or-beginning-search | |
− | + | zle -N up-line-or-beginning-search | |
+ | zle -N down-line-or-beginning-search | ||
− | + | [[ -n "$key[Up]" ]] && bindkey -- "$key[Up]" up-line-or-beginning-search | |
+ | [[ -n "$key[Down]" ]] && bindkey -- "$key[Down]" down-line-or-beginning-search | ||
+ | </nowiki>}} | ||
− | {{ | + | By doing this, only the past commands matching the current line up to the current cursor position will be shown when {{ic|Up}} or {{ic|Down}} keys are pressed. |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | }} | ||
− | + | === Prompts === | |
− | + | Zsh supports: | |
− | + | * left side prompts: {{ic|PS1}} (or {{ic|PROMPT}}), {{ic|PS2}}, {{ic|PS3}}, {{ic|PS4}}. | |
+ | * right side prompts: {{ic|RPS1}} (or {{ic|RPROMPT}}), {{ic|RPS2}} (or {{ic|RPROMPT2}}). | ||
− | + | See [http://zsh.sourceforge.net/Doc/Release/Parameters.html#Parameters-Used-By-The-Shell Parameters Used By The Shell] or {{man|1|zshparam}} for their explanation. | |
− | {{ | ||
− | |||
− | === | + | ==== 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 {{ic|.zshrc}}. This can be done by adding these lines to: | |
− | |||
− | + | {{hc|~/.zshrc| | |
− | {{hc|~/.zshrc| | + | autoload -Uz promptinit |
− | + | promptinit | |
− | + | }} | |
− | |||
− | + | Available prompt themes are listed by running the command: | |
− | + | $ prompt -l | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | For example, to use the {{ic|walters}} theme, enter: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | $ prompt walters | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | To preview all available themes, use this command: | |
− | + | $ prompt -p | |
− | + | ==== Customized prompt ==== | |
− | {{ | + | For users who are dissatisfied with the prompt themes 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}} manpage. |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | }} | ||
− | ==== | + | ===== Colors ===== |
− | + | Zsh sets colors differently than [[Color_Bash_Prompt|Bash]]. See [http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Visual-effects Visual effects] or {{man|1|zshmisc}} for prompt escapes to set foreground color, background color and other visual effects. | |
− | {{ | ||
− | |||
− | |||
− | |||
− | }} | ||
− | + | [http://zsh.sourceforge.net/FAQ/zshfaq03.html#l42 Colors] can be specified by numeric color code or by name (see [http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Character-Highlighting Character Highlighting] or {{man|1|zshzle}}). Most terminals support the following colors by name: | |
− | + | {| class="wikitable" | |
+ | |- | ||
+ | ! colspan="2" | Possible color values | ||
+ | |- | ||
+ | | {{ic|black}} or {{ic|0}} || {{ic|red}} or {{ic|1}} | ||
+ | |- | ||
+ | | {{ic|green}} or {{ic|2}} || {{ic|yellow}} or {{ic|3}} | ||
+ | |- | ||
+ | | {{ic|blue}} or {{ic|4}} || {{ic|magenta}} or {{ic|5}} | ||
+ | |- | ||
+ | | {{ic|cyan}} or {{ic|6}} || {{ic|white}} or {{ic|7}} | ||
+ | |} | ||
− | + | See [https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg xterm-256color chart] for color codes supported by terminal emulators compatible with xterm 256 colors. | |
− | |||
− | |||
− | |||
− | + | {{Note| Bold text does not necessarily use the same colors as normal text. For example, {{ic|%F{yellow}''text''%f}} looks brown or a very dark yellow, while {{ic|%F{yellow}%B''text''%b%f}} looks like bright or regular yellow.}} | |
− | + | {{Tip|Prompt escapes can be tested with command {{ic|print -P ''"prompt escapes"''}}, for example: | |
− | + | $ print -P '%B%F{red}co%F{green}lo%F{blue}rs%f%b' | |
− | |||
− | |||
− | |||
}} | }} | ||
− | + | ===== Example ===== | |
− | |||
− | |||
− | + | This is an example of a two-sided prompt: | |
− | + | PROMPT='%F{red}%n%f@%F{blue}%m%f %F{yellow}%1~%f %# ' | |
+ | RPROMPT='[%F{yellow}%?%f]' | ||
− | + | And here is how it will be displayed: | |
− | + | username@host ~ % [0] | |
− | === | + | === Sample .zshrc files === |
− | + | * To get the same setup as the [https://www.archlinux.org/download/ monthly ISO releases] (which use Zsh by default), install {{Pkg|grml-zsh-config}}. It includes the many tweaks and advanced optimizations from [https://grml.org/zsh/ grml]. | |
+ | * https://github.com/MrElendig/dotfiles-alice/blob/master/.zshrc - basic setup, with dynamic prompt and window title/hardinfo. | ||
+ | * https://github.com/slashbeast/conf-mgmt-like-a-sir/blob/master/roles/zsh/files/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. | ||
− | + | See [[dotfiles#Repositories]] for more. | |
− | === | + | === Configuration Frameworks === |
− | + | * {{App|Antigen|A plugin manager for zsh, inspired by oh-my-zsh and vundle.|https://github.com/zsh-users/antigen|{{AUR|antigen-git}}}} | |
− | + | * {{App|oh-my-zsh|A popular, community-driven framework for managing your Zsh configuration. It comes bundled with a ton of helpful functions, helpers, plugins, themes.|https://github.com/robbyrussell/oh-my-zsh|{{AUR|oh-my-zsh-git}}}} | |
− | |||
− | |||
− | |||
− | |||
− | + | :{{Note|For themes to work you may need to set {{ic|setopt NO_GLOBAL_RCS}} in the {{ic|~/.zshenv}} file, otherwise changes to some variables (such as {{ic|$PROMPT}}) may be overwritten.}} | |
− | + | * {{App|Prezto|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.|https://github.com/sorin-ionescu/prezto|{{AUR|prezto-git}}}} | |
− | |||
− | |||
− | == | + | == Tips and tricks == |
− | + | === Autostart X at login === | |
− | |||
− | + | See [[xinit#Autostart X at login]]. | |
− | |||
− | === | + | === The "command not found" hook === |
− | + | [[pkgfile]] includes a "command not found" hook that will automatically search the official repositories, when entering an unrecognized command. | |
− | |||
− | |||
− | |||
− | |||
− | + | You need to [[source]] the hook to enable it, for example: | |
− | + | {{hc|~/.zshrc| | |
− | + | source /usr/share/doc/pkgfile/command-not-found.zsh | |
+ | }} | ||
− | + | === The ttyctl command === | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | [http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html#index-tty_002c-freezing] describes the {{ic|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: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |} | ||
− | |||
− | + | {{hc|~/.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: | |
− | + | {{hc|~/.zshrc|<nowiki> | |
− | |||
− | {{hc|.zshrc|<nowiki> | ||
DIRSTACKFILE="$HOME/.cache/zsh/dirs" | DIRSTACKFILE="$HOME/.cache/zsh/dirs" | ||
if [[ -f $DIRSTACKFILE ]] && [[ $#dirstack -eq 0 ]]; then | if [[ -f $DIRSTACKFILE ]] && [[ $#dirstack -eq 0 ]]; then | ||
Line 353: | Line 289: | ||
DIRSTACKSIZE=20 | DIRSTACKSIZE=20 | ||
− | setopt | + | setopt AUTO_PUSHD PUSHD_SILENT PUSHD_TO_HOME |
## Remove duplicate entries | ## Remove duplicate entries | ||
− | setopt | + | setopt PUSHD_IGNORE_DUPS |
## This reverts the +/- operators. | ## This reverts the +/- operators. | ||
− | setopt | + | setopt PUSHD_MINUS |
</nowiki>}} | </nowiki>}} | ||
− | Now | + | Now use |
− | dirs -v | + | |
− | to print the dirstack. Use {{ic|cd -<NUM>}} to go back to a visited folder. | + | $ dirs -v |
+ | |||
+ | to print the dirstack. Use {{ic|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 {{ic|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. | ||
+ | |||
+ | See [http://zsh.sourceforge.net/Doc/Release/User-Contributions.html#Recent-Directories Remembering Recent Directories] or {{man|1|zshcontrib}}. | ||
+ | |||
+ | === Help command === | ||
+ | |||
+ | Zsh {{ic|help}} command is called {{ic|run-help}}. Unlike [[bash]], ''zsh'' does not enable it by default. To use {{ic|help}} in zsh, add following to your {{ic|zshrc}}: | ||
+ | |||
+ | {{bc|1= | ||
+ | autoload -Uz run-help | ||
+ | unalias run-help | ||
+ | alias help=run-help | ||
+ | }} | ||
+ | |||
+ | {{ic|run-help}} will invoke ''man'' for external commands. Default keyboard shortcut is {{ic|Alt+h}} or {{ic|Esc+h}}. | ||
+ | |||
+ | {{ic|run-help}} has helper functions, they need to be enabled separately: | ||
+ | |||
+ | {{bc|1= | ||
+ | autoload -Uz run-help-git | ||
+ | autoload -Uz run-help-ip | ||
+ | autoload -Uz run-help-openssl | ||
+ | autoload -Uz run-help-p4 | ||
+ | autoload -Uz run-help-sudo | ||
+ | autoload -Uz run-help-svk | ||
+ | autoload -Uz run-help-svn | ||
+ | }} | ||
− | === | + | For example {{ic|run-help git commit}} command will now open the [[man page]] {{man|1|git-commit}} instead of {{man|1|git}}. |
+ | |||
+ | === Fish-like syntax highlighting === | ||
+ | |||
+ | [[Fish]] provides a very powerful shell syntax highlighting. To use this in zsh, you can install {{pkg|zsh-syntax-highlighting}} from offical repository and add following to your zshrc: | ||
+ | |||
+ | source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh | ||
+ | |||
+ | === Persistent rehash === | ||
+ | |||
+ | Typically, compinit will not automatically find new executables in the {{ic|$PATH}}. For example, after you install a new package, the files in {{ic|/usr/bin/}} would not be immediately or automatically included in the completion. Thus, to have these new executables included, one would run: | ||
+ | |||
+ | $ rehash | ||
+ | |||
+ | This 'rehash' can be set to happen automatically.[https://github.com/robbyrussell/oh-my-zsh/issues/3440] Simply include the following in your {{ic|zshrc}}: | ||
+ | |||
+ | {{hc|~/.zshrc| | ||
+ | zstyle ':completion:*' rehash true | ||
+ | }} | ||
+ | |||
+ | === Bind key to ncurses application === | ||
+ | |||
+ | Bind a ncurses application to a keystroke, but it will not accept interaction. Use {{ic|BUFFER}} variable to make it work. The following example lets users open ncmpcpp using {{ic|Alt+\}}: | ||
+ | |||
+ | {{hc|~/.zshrc|2= | ||
+ | ncmpcppShow() { BUFFER="ncmpcpp"; zle accept-line; } | ||
+ | zle -N ncmpcppShow | ||
+ | bindkey '^[\' ncmpcppShow | ||
+ | }} | ||
+ | |||
+ | An alternate method, that will keep everything you entered in the line before calling application: | ||
+ | |||
+ | {{hc|~/.zshrc|2= | ||
+ | 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 ({{ic|Alt+Left}}), the second let the user go to the parent directory ({{ic|Alt+Up}}). They also display the directory content. | ||
+ | |||
+ | {{hc|~/.zshrc|<nowiki> | ||
+ | cdUndoKey() { | ||
+ | popd | ||
+ | zle reset-prompt | ||
+ | echo | ||
+ | ls | ||
+ | zle reset-prompt | ||
+ | } | ||
+ | |||
+ | cdParentKey() { | ||
+ | pushd .. | ||
+ | zle reset-prompt | ||
+ | echo | ||
+ | ls | ||
+ | zle reset-prompt | ||
+ | } | ||
+ | |||
+ | zle -N cdParentKey | ||
+ | zle -N cdUndoKey | ||
+ | bindkey '^[[1;3A' cdParentKey | ||
+ | bindkey '^[[1;3D' cdUndoKey | ||
+ | </nowiki>}} | ||
+ | |||
+ | === xterm title === | ||
+ | |||
+ | If your terminal emulator supports it you can set its title from Zsh. This allows dynamically changing the the title to display relevant information about the shell state, for example showing the user name and current directory or the currently executing command. | ||
+ | |||
+ | xterm title is set with [http://tldp.org/HOWTO/Xterm-Title-3.html#ss3.1 xterm escape sequences]. For example: | ||
+ | |||
+ | $ print -n '\e]2;My xterm title\a' | ||
− | + | will set the title to | |
− | + | My xterm title | |
− | |||
− | |||
− | |||
− | + | An simple way to have a dynamic title is to set the title in a hook functions {{ic|precmd}} and {{ic|preexec}}. See [http://zsh.sourceforge.net/Doc/Release/Functions.html#Hook-Functions Hook Functions] or {{man|1|zshmisc}}. | |
− | + | By using {{ic|print -P}} you can take advantage of prompt escapes. | |
− | + | {{Tip|Title printing can be split up in multiple commands as long as they are sequential.}} | |
− | + | {{Note| | |
+ | * Do not use {{ic|-P}} option of {{ic|print}} when printing variables to prevent them from being parsed as prompt escapes. | ||
+ | * Use {{ic|q}} [http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags parameter expansion flag] when printing variables to prevent them from being parsed as escape sequences. | ||
+ | }} | ||
− | + | {{hc|~/.zshrc|<nowiki> | |
+ | autoload -Uz add-zsh-hook | ||
− | + | function xterm_title_precmd () { | |
+ | print -Pn '\e]2;%n@%m %1~\a' | ||
+ | } | ||
− | + | function xterm_title_preexec () { | |
+ | print -Pn '\e]2;%n@%m %1~ %# ' | ||
+ | print -n "${(q)1}\a" | ||
+ | } | ||
− | + | if [[ "$TERM" == (screen*|xterm*|rxvt*) ]]; then | |
+ | add-zsh-hook -Uz precmd xterm_title_precmd | ||
+ | add-zsh-hook -Uz preexec xterm_title_preexec | ||
+ | fi | ||
+ | </nowiki>}} | ||
== Uninstallation == | == Uninstallation == | ||
− | + | Change the default shell before removing the {{Pkg|zsh}} package. | |
− | {{Warning|Failure to follow the below | + | {{Warning|Failure to follow the below procedure may result in users no longer having access to a working shell.}} |
− | + | 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 {{Pkg|zsh}} package can be removed. | |
− | {{ | + | Alternatively, change the default shell back to Bash by editing {{ic|/etc/passwd}} as root. |
− | + | {{Warning|It is strongly recommended to use {{ic|vipw}} when editing {{ic|/etc/passwd}} as it helps prevent invalid entries and/or syntax errors.}} | |
− | + | For example, change the following: | |
− | ''username'':x:1000:1000:Full Name,,,:/home/''username'':/bin/zsh | + | |
− | + | ''username'':x:1000:1000:''Full Name'',,,:/home/''username'':/bin/zsh | |
− | ''username'':x:1000:1000:Full Name,,,:/home/''username'':/bin/bash | + | |
+ | To this: | ||
+ | |||
+ | ''username'':x:1000:1000:''Full Name'',,,:/home/''username'':/bin/bash | ||
== See also == | == See also == | ||
− | + | * [http://zsh.sourceforge.net/Guide/zshguide.html A User's Guide to ZSH] | |
− | *[http://zsh.sourceforge.net/Guide/zshguide.html | + | * [http://zsh.sourceforge.net/Doc/Release/index-frame.html The Z Shell Manual] (different format available [http://zsh.sourceforge.net/Doc/ here]) |
− | *[http://zsh.sourceforge.net/Doc/Release/index-frame.html | + | * [http://zsh.sourceforge.net/FAQ/zshfaq01.html Zsh FAQ] |
− | *[http://zsh.sourceforge.net/FAQ/zshfaq01.html Zsh FAQ] | + | * [http://zshwiki.org/home/ Zsh Wiki] |
− | *[http://zshwiki.org/home/ Zsh | + | * {{man|1|zsh-lovers}} (available as {{pkg|zsh-lovers}} package) |
− | * | + | * [[Gentoo: Zsh/Guide]] |
− | *[http://www.bash2zsh.com/zsh_refcard/refcard.pdf Bash2Zsh Reference Card] | + | * [http://www.bash2zsh.com/zsh_refcard/refcard.pdf Bash2Zsh Reference Card] |
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 23:15, 25 April 2018
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.
The Zsh FAQ offers more reasons to use Zsh.
Contents
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.
~/.bashrc
to ~/.zshrc
(e.g. the prompt and the aliases) and from ~/.bash_profile
to ~/.zprofile
(e.g. the code that starts the X Window System).Startup/Shutdown files
- If
$ZDOTDIR
is not set,$HOME
is used instead. - 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 shell exits. -
/etc/zsh/zlogout
Will be sourced when a login shell exits.
- 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
.
/etc/zsh/zprofile
with something other, it will break the integrity 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
.
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
.
To prevent your $PATH
being overwritten, set it in ~/.zprofile
.
~/.zprofile
typeset -U path path=(~/.local/bin /other/things/in/path $path[@])
See also A User's Guide to the Z-Shell and the note in #Startup/Shutdown 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 -Uz compinit compinit
The above configuration includes ssh/scp/sftp hostnames completion but in order for this feature to work, users must not enable ssh's hostname hashing (i.e. option HashKnownHosts
in ssh client configuration).
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.
See ZshWiki: zle:bindkeys for instructions on keybinding setup.
History search
You need to set up #Key bindings to use this. To enable history search add these lines to .zshrc
file:
~/.zshrc
autoload -Uz up-line-or-beginning-search down-line-or-beginning-search zle -N up-line-or-beginning-search zle -N down-line-or-beginning-search [[ -n "$key[Up]" ]] && bindkey -- "$key[Up]" up-line-or-beginning-search [[ -n "$key[Down]" ]] && bindkey -- "$key[Down]" down-line-or-beginning-search
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
Zsh supports:
- left side prompts:
PS1
(orPROMPT
),PS2
,PS3
,PS4
. - right side prompts:
RPS1
(orRPROMPT
),RPS2
(orRPROMPT2
).
See Parameters Used By The Shell or zshparam(1) for their explanation.
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 prompt themes 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.
See Prompt Expansion for a list of prompt variables and conditional substrings, or take a look at the zshmisc(1) manpage.
Colors
Zsh sets colors differently than Bash. See Visual effects or zshmisc(1) for prompt escapes to set foreground color, background color and other visual effects.
Colors can be specified by numeric color code or by name (see Character Highlighting or zshzle(1)). Most terminals support the following colors by name:
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
|
See xterm-256color chart for color codes supported by terminal emulators compatible with xterm 256 colors.
%F{yellow}text%f
looks brown or a very dark yellow, while %F{yellow}%Btext%b%f
looks like bright or regular yellow.print -P "prompt escapes"
, for example:
$ print -P '%B%F{red}co%F{green}lo%F{blue}rs%f%b'
Example
This is an example of a two-sided prompt:
PROMPT='%F{red}%n%f@%F{blue}%m%f %F{yellow}%1~%f %# ' RPROMPT='[%F{yellow}%?%f]'
And here is how it will be displayed:
username@host ~ % [0]
Sample .zshrc files
- To get the same setup as the monthly ISO releases (which use Zsh by default), install grml-zsh-config. It includes the many tweaks and advanced optimizations from grml.
- https://github.com/MrElendig/dotfiles-alice/blob/master/.zshrc - basic setup, with dynamic prompt and window title/hardinfo.
- https://github.com/slashbeast/conf-mgmt-like-a-sir/blob/master/roles/zsh/files/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.
See dotfiles#Repositories for more.
Configuration Frameworks
- Antigen — A plugin manager for zsh, inspired by oh-my-zsh and vundle.
- oh-my-zsh — A popular, community-driven framework for managing your Zsh configuration. It comes bundled with a ton of helpful functions, helpers, plugins, themes.
- Note: For themes to work you may need to set
setopt NO_GLOBAL_RCS
in the~/.zshenv
file, otherwise changes to some variables (such as$PROMPT
) may be overwritten.
- Prezto — 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.
Tips and tricks
Autostart X at login
See xinit#Autostart X at login.
The "command not found" hook
pkgfile includes a "command not found" hook that will automatically search the official repositories, when entering an unrecognized command.
You need to source the hook to enable it, for example:
~/.zshrc
source /usr/share/doc/pkgfile/command-not-found.zsh
The ttyctl command
[1] 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:
~/.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 AUTO_PUSHD PUSHD_SILENT PUSHD_TO_HOME ## Remove duplicate entries setopt PUSHD_IGNORE_DUPS ## This reverts the +/- operators. setopt PUSHD_MINUS
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.
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.
See Remembering Recent Directories or zshcontrib(1).
Help command
Zsh help
command is called run-help
. Unlike bash, zsh does not enable it by default. To use help
in zsh, add following to your zshrc
:
autoload -Uz run-help unalias run-help alias help=run-help
run-help
will invoke man for external commands. Default keyboard shortcut is Alt+h
or Esc+h
.
run-help
has helper functions, they need to be enabled separately:
autoload -Uz run-help-git autoload -Uz run-help-ip autoload -Uz run-help-openssl autoload -Uz run-help-p4 autoload -Uz run-help-sudo autoload -Uz run-help-svk autoload -Uz run-help-svn
For example run-help git commit
command will now open the man page git-commit(1) instead of git(1).
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
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 executables included, one would run:
$ rehash
This 'rehash' can be set to happen automatically.[2] Simply include the following in your zshrc
:
~/.zshrc
zstyle ':completion:*' rehash true
Bind key to ncurses application
Bind a ncurses application to a keystroke, 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
An alternate method, that 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 zle reset-prompt echo ls zle reset-prompt } cdParentKey() { pushd .. zle reset-prompt echo ls zle reset-prompt } zle -N cdParentKey zle -N cdUndoKey bindkey '^[[1;3A' cdParentKey bindkey '^[[1;3D' cdUndoKey
xterm title
If your terminal emulator supports it you can set its title from Zsh. This allows dynamically changing the the title to display relevant information about the shell state, for example showing the user name and current directory or the currently executing command.
xterm title is set with xterm escape sequences. For example:
$ print -n '\e]2;My xterm title\a'
will set the title to
My xterm title
An simple way to have a dynamic title is to set the title in a hook functions precmd
and preexec
. See Hook Functions or zshmisc(1).
By using print -P
you can take advantage of prompt escapes.
- Do not use
-P
option ofprint
when printing variables to prevent them from being parsed as prompt escapes. - Use
q
parameter expansion flag when printing variables to prevent them from being parsed as escape sequences.
~/.zshrc
autoload -Uz add-zsh-hook function xterm_title_precmd () { print -Pn '\e]2;%n@%m %1~\a' } function xterm_title_preexec () { print -Pn '\e]2;%n@%m %1~ %# ' print -n "${(q)1}\a" } if [[ "$TERM" == (screen*|xterm*|rxvt*) ]]; then add-zsh-hook -Uz precmd xterm_title_precmd add-zsh-hook -Uz preexec xterm_title_preexec fi
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.
vipw
when editing /etc/passwd
as it helps prevent invalid entries and/or syntax errors.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 Wiki
- zsh-lovers(1) (available as zsh-lovers package)
- Gentoo: Zsh/Guide
- Bash2Zsh Reference Card