zshzle (1) Linux Manual Page
NAME
zshzle – zsh command line editor
DESCRIPTION
If the ZLE option is set (which it is by default in interactive shells) and the shell input is attached to the terminal, the user is able to edit command lines.
There are two display modes. The first, multiline mode, is the default. It only works if the TERM parameter is set to a valid terminal type that can move the cursor up. The second, single line mode, is used if TERM is invalid or incapable of moving the cursor up, or if the SINGLE_LINE_ZLE option is set. This mode is similar to ksh, and uses no termcap sequences. If TERM is "emacs", the ZLE option will be unset by default.
The parameters BAUD, COLUMNS, and LINES are also used by the line editor. See Parameters Used By The Shell in zshparam(1).
The parameter zle_highlight is also used by the line editor; see Character Highlighting below. Highlighting of special characters and the region between the cursor and the mark (as set with set-mark-command in Emacs mode, or by visual-mode in Vi mode) is enabled by default; consult this reference for more information. Irascible conservatives will wish to know that all highlighting may be disabled by the following setting:
-
zle_highlight=(none)
In many places, references are made to the numeric argument. This can by default be entered in emacs mode by holding the alt key and typing a number, or pressing escape before each digit, and in vi command mode by typing the number before entering a command. Generally the numeric argument causes the next command entered to be repeated the specified number of times, unless otherwise noted below; this is implemented by the digit-argument widget. See also the Arguments subsection of the Widgets section for some other ways the numeric argument can be modified.
KEYMAPS
A keymap in ZLE contains a set of bindings between key sequences and ZLE commands. The empty key sequence cannot be bound.
There can be any number of keymaps at any time, and each keymap has one or more names. If all of a keymap’s names are deleted, it disappears. bindkey can be used to manipulate keymap names.
Initially, there are eight keymaps:
emacs- EMACS emulation
viins- vi emulation – insert mode
vicmd- vi emulation – command mode
viopp- vi emulation – operator pending
visual- vi emulation – selection active
isearch- incremental search mode
command- read a command name
.safe- fallback keymap
The `.safe‘ keymap is special. It can never be altered, and the name can never be removed. However, it can be linked to other names, which can be removed. In the future other special keymaps may be added; users should avoid using names beginning with `.‘ for their own keymaps.
In addition to these names, either `emacs‘ or `viins‘ is also linked to the name `main‘. If one of the VISUAL or EDITOR environment variables contain the string `vi‘ when the shell starts up then it will be `viins‘, otherwise it will be `emacs‘. bindkey‘s -e and -v options provide a convenient way to override this default choice.
When the editor starts up, it will select the `main‘ keymap. If that keymap doesn’t exist, it will use `.safe‘ instead.
In the `.safe‘ keymap, each single key is bound to self-insert, except for ^J (line feed) and ^M (return) which are bound to accept-line. This is deliberately not pleasant to use; if you are using it, it means you deleted the main keymap, and you should put it back.
Reading Commands
When ZLE is reading a command from the terminal, it may read a sequence that is bound to some command and is also a prefix of a longer bound string. In this case ZLE will wait a certain time to see if more characters are typed, and if not (or they don’t match any longer string) it will execute the binding. This timeout is defined by the KEYTIMEOUT parameter; its default is 0.4 sec. There is no timeout if the prefix string is not itself bound to a command.
The key timeout is also applied when ZLE is reading the bytes from a multibyte character string when it is in the appropriate mode. (This requires that the shell was compiled with multibyte mode enabled; typically also the locale has characters with the UTF-8 encoding, although any multibyte encoding known to the operating system is supported.) If the second or a subsequent byte is not read within the timeout period, the shell acts as if ? were typed and resets the input state.
As well as ZLE commands, key sequences can be bound to other strings, by using `bindkey -s‘. When such a sequence is read, the replacement string is pushed back as input, and the command reading process starts again using these fake keystrokes. This input can itself invoke further replacement strings, but in order to detect loops the process will be stopped if there are twenty such replacements without a real command being read.
A key sequence typed by the user can be turned into a command name for use in user-defined widgets with the read-command widget, described in the subsection `Miscellaneous’ of the section `Standard Widgets’ below.
Local Keymaps
While for normal editing a single keymap is used exclusively, in many modes a local keymap allows for some keys to be customised. For example, in an incremental search mode, a binding in the isearch keymap will override a binding in the main keymap but all keys that are not overridden can still be used.
If a key sequence is defined in a local keymap, it will hide a key sequence in the global keymap that is a prefix of that sequence. An example of this occurs with the binding of iw in viopp as this hides the binding of i in vicmd. However, a longer sequence in the global keymap that shares the same prefix can still apply so for example the binding of ^Xa in the global keymap will be unaffected by the binding of ^Xb in the local keymap.
ZLE BUILTINS
The ZLE module contains three related builtin commands. The bindkey command manipulates keymaps and key bindings; the vared command invokes ZLE on the value of a shell parameter; and the zle command manipulates editing widgets and allows command line access to ZLE commands from within shell functions.
-
bindkey[ options ]-l[-L] [ keymap … ] -
bindkey[ options ]-d -
bindkey[ options ]-Dkeymap … -
bindkey[ options ]-Aold-keymap new-keymap -
bindkey[ options ]-Nnew-keymap [ old-keymap ] -
bindkey[ options ]-m -
bindkey[ options ]-rin-string … -
bindkey[ options ]-sin-string out-string … -
bindkey[ options ] in-string command … -
bindkey[ options ] [ in-string ]bindkey‘s options can be divided into three categories: keymap selection for the current command, operation selection, and others. The keymap selection options are:-
-
-eSelects keymap `emacs‘ for any operations by the current command, and also links `emacs‘ to `main‘ so that it is selected by default the next time the editor starts. -v- Selects keymap `
viins‘ for any operations by the current command, and also links `viins‘ to `main‘ so that it is selected by default the next time the editor starts. -a- Selects keymap `
vicmd‘ for any operations by the current command. -Mkeymap- The keymap specifies a keymap name that is selected for any operations by the current command.
If a keymap selection is required and none of the options above are used, the `
main‘ keymap is used. Some operations do not permit a keymap to be selected, namely:-
-lList all existing keymap names; if any arguments are given, list just those keymaps.-
If the
-Loption is also used, list in the form ofbindkeycommands to create or link the keymaps. `bindkey -lL main‘ shows which keymap is linked to `main‘, if any, and hence if the standard emacs or vi emulation is in effect. This option does not show the.safekeymap because it cannot be created in that fashion; however, neither is `bindkey -lL .safe‘ reported as an error, it simply outputs nothing.
-d- Delete all existing keymaps and reset to the default state.
-Dkeymap …- Delete the named keymaps.
-Aold-keymap new-keymap- Make the new-keymap name an alias for old-keymap, so that both names refer to the same keymap. The names have equal standing; if either is deleted, the other remains. If there is already a keymap with the new-keymap name, it is deleted.
-Nnew-keymap [ old-keymap ]- Create a new keymap, named new-keymap. If a keymap already has that name, it is deleted. If an old-keymap name is given, the new keymap is initialized to be a duplicate of it, otherwise the new keymap will be empty.
To use a newly created keymap, it should be linked to
main. Hence the sequence of commands to create and use a new keymap `mymap‘ initialized from theemacskeymap (which remains unchanged) is:-
bindkey -N mymap emacs bindkey -A mymap main
Note that while `
bindkey -Anewmapmain‘ will work when newmap isemacsorviins, it will not work forvicmd, as switching from vi insert to command mode becomes impossible.The following operations act on the `
main‘ keymap if no keymap selection option was given:-
-mAdd the built-in set of meta-key bindings to the selected keymap. Only keys that are unbound or bound toself-insertare affected. -rin-string …- Unbind the specified in-strings in the selected keymap. This is exactly equivalent to binding the strings to
undefined-key.-
When
-Ris also used, interpret the in-strings as ranges.When
-pis also used, the in-strings specify prefixes. Any binding that has the given in-string as a prefix, not including the binding for the in-string itself, if any, will be removed. For example,-
bindkey -rpM viins '^['
will remove all bindings in the vi-insert keymap beginning with an escape character (probably cursor keys), but leave the binding for the escape character itself (probably
vi-cmd-mode). This is incompatible with the option-R.
-sin-string out-string …- Bind each in-string to each out-string. When in-string is typed, out-string will be pushed back and treated as input to the line editor. When
-Ris also used, interpret the in-strings as ranges.-
Note that both in-string and out-string are subject to the same form of interpretation, as described below.
- in-string command …
- Bind each in-string to each command. When
-Ris used, interpret the in-strings as ranges. - [ in-string ]
- List key bindings. If an in-string is specified, the binding of that string in the selected keymap is displayed. Otherwise, all key bindings in the selected keymap are displayed. (As a special case, if the
-eor-voption is used alone, the keymap is not displayed – the implicit linking of keymaps is the only thing that happens.)-
When the option
-pis used, the in-string must be present. The listing shows all bindings which have the given key sequence as a prefix, not including any bindings for the key sequence itself.When the
-Loption is used, the list is in the form ofbindkeycommands to create the key bindings.
When the
-Roption is used as noted above, a valid range consists of two characters, with an optional `–‘ between them. All characters between the two specified, inclusive, are bound as specified.For either in-string or out-string, the following escape sequences are recognised:
- bell character
- backspace
,\E- escape
- form feed
- linefeed (newline)
- carriage return
- horizontal tab
- vertical tab
\NNN- character code in octal
