ZLE Functions
=============
Widgets
-------
These functions all implement user-defined ZLE widgets (see Note:Zsh
Line Editor) which can be bound to keystrokes in interactive shells.
To use them, your .zshrc should contain lines of the form
autoload FUNCTION
zle -N FUNCTION
followed by an appropriate bindkey command to associate the function
with a key sequence. Suggested bindings are described below.
bash-forward-word, bash-backward-word
bash-kill-word, bash-backward-kill-word
bash-up-case-word, bash-down-case-word
bash-transpose-words
These work similarly to the corresponding builtin zle functions
without the `bash-' prefix, but a word is considered to consist of
alphanumeric characters only. If you wish to replace your
existing bindings with these four widgets, the following is
sufficient:
for widget in kill-word backward-kill-word \
forward-word backward-word \
up-case-word down-case-word \
transpose-words; do
autoload bash-$widget
zle -N $widget bash-$widget
done
cycle-completion-positions
After inserting an unambiguous string into the command line, the
new function based completion system may know about multiple
places in this string where characters are missing or differ from
at least one of the possible matches. It will then place the
cursor on the position it considers to be the most interesting
one, i.e. the one where one can disambiguate between as many
matches as possible with as little typing as possible.
This widget allows the cursor to be easily moved to the other
interesting spots. It can be invoked repeatedly to cycle between
all positions reported by the completion system.
edit-command-line
Edit the command line using your visual editor, as in ksh.
bindkey -M vicmd v edit-command-line
history-search-end
This function implements the widgets
history-beginning-search-backward-end and
history-beginning-search-forward-end. These commands work by first
calling the corresponding builtin widget (see Note:History
Control) and then moving the cursor to the end of the line. The
original cursor position is remembered and restored before calling
the builtin widget a second time, so that the same search is
repeated to look farther through the history.
Although you autoload only one function, the commands to use it are
slightly different because it implements two widgets.
zle -N history-beginning-search-backward-end \
history-search-end
zle -N history-beginning-search-forward-end \
history-search-end
bindkey '\e^P' history-beginning-search-backward-end
bindkey '\e^N' history-beginning-search-forward-end
incarg
Typing the keystrokes for this widget with the cursor placed on or
to the left of an integer causes that integer to be incremented by
one. With a numeric prefix argument, the number is incremented by
the amount of the argument (decremented if the prefix argument is
negative). The shell parameter incarg may be set to change the
default increment something other than one.
bindkey '^X+' incarg
incremental-complete-word
This allows incremental completion of a word. After starting this
command, a list of completion choices can be shown after every
character you type, which you can delete with ^H or DEL. Pressing
return accepts the completion so far and returns you to normal
editing (that is, the command line is _not_ immediately executed).
You can hit TAB to do normal completion, ^G to abort back to the
state when you started, and ^D to list the matches.
This works only with the new function based completion system.
bindkey '^Xi' incremental-complete-word
insert-files
This function allows you type a file pattern, and see the results
of the expansion at each step. When you hit return, all
expansions are inserted into the command line.
bindkey '^Xf' insert-files
predict-on
This set of functions implements predictive typing using history
search. After predict-on, typing characters causes the editor to
look backward in the history for the first line beginning with
what you have typed so far. After predict-off, editing returns to
normal for the line found. In fact, you often don't even need to
use predict-off, because if the line doesn't match something in
the history, adding a key performs standard completion, and then
inserts itself if no completions were found. However, editing in
the middle of a line is liable to confuse prediction; see the
toggle style below.
With the function based completion system (which is needed for
this), you should be able to type TAB at almost any point to
advance the cursor to the next ``interesting'' character position
(usually the end of the current word, but sometimes somewhere in
the middle of the word). And of course as soon as the entire line
is what you want, you can accept with return, without needing to
move the cursor to the end first.
The first time predict-on is used, it creates several additional
widget functions:
delete-backward-and-predict
Replaces the backward-delete-char widget. You do not need to
bind this yourself.
insert-and-predict
Implements predictive typing by replacing the self-insert
widget. You do not need to bind this yourself.
predict-off
Turns off predictive typing.
Although you autoload only the predict-on function, it is
necessary to create a keybinding for predict-off as well.
zle -N predict-on
zle -N predict-off
bindkey '^X^Z' predict-on
bindkey '^Z' predict-off
smart-insert-last-word
This function may replace the insert-last-word widget, like so:
zle -N insert-last-word smart-insert-last-word
With a numeric prefix, it behaves like insert-last-word, except
that words in comments are ignored when INTERACTIVE_COMMENTS is
set.
Otherwise, the rightmost ``interesting'' word from the previous
command is found and inserted. The default definition of
``interesting'' is that the word contains at least one alphabetic
character, slash, or backslash. This definition may be overridden
by use of the match style. The context used to look up the style
is the widget name, so usually the context is :insert-last-word.
However, you can bind this function to different widgets to use
different patterns:
zle -N insert-last-assignment smart-insert-last-word
zstyle :insert-last-assignment match '[[:alpha:]][][[:alnum:]]#=*'
bindkey '\e=' insert-last-assignment
Styles
------
The behavior of several of the above widgets can be controlled by the
use of the zstyle mechanism. In particular, widgets that interact with
the completion system pass along their context to any completions that
they invoke.
break-keys
This style is used by the incremental-complete-word widget. Its
value should be a pattern, and all keys matching this pattern will
cause the widget to stop incremental completion without the key
having any further effect. Like all styles used directly by
incremental-complete-word, this style is looked up using the
context `:incremental'.
completer
The incremental-complete-word and insert-and-predict widgets set
up their top-level context name before calling completion. This
allows one to define different sets of completer functions for
normal completion and for these widgets. For example, to use
completion, approximation and correction for normal completion,
completion and correction for incremental completion and only
completion for prediction one could use:
zstyle ':completion:*' completer \
_complete _correct _approximate
zstyle ':completion:incremental:*' completer \
_complete _correct
zstyle ':completion:predict:*' completer \
_complete
It is a good idea to restrict the completers used in prediction,
because they may be automatically invoked as you type. The _list
and _menu completers should never be used with prediction. The
_approximate, _correct, _expand, and _match completers may be
used, but be aware that they may change characters anywhere in the
word behind the cursor, so you need to watch carefully that the
result is what you intended.
cursor
The insert-and-predict widget uses this style, in the context
`:predict', to decide where to place the cursor after completion
has been tried. Values are:
complete
The cursor is left where it was when completion finished, but
only if it is after a character equal to the one just
inserted by the user. If it is after another character, this
value is the same as `key'.
key
The cursor is left after the Nth occurrence of the character
just inserted, where N is the number of times that character
appeared in the word before completion was attempted. In
short, this has the effect of leaving the cursor after the
character just typed even if the completion code found out
that no other characters need to be inserted at that position.
Any other value for this style unconditionally leaves the cursor
at the position where the completion code left it.
list
When using the incremental-complete-word widget, this style says
if the matches should be listed on every key press (if they fit on
the screen). Use the context prefix `:completion:incremental'.
The insert-and-predict widget uses this style to decide if the
completion should be shown even if there is only one possible
completion. This is done if the value of this style is the string
always. In this case the context is `:predict' (_not_
`:completion:predict').
match
This style is used by smart-insert-last-word to provide a pattern
(using full EXTENDED_GLOB syntax) that matches an interesting word.
The context is the name of the widget to which
smart-insert-last-word is bound (see above). The default behavior
of smart-insert-last-word is equivalent to:
zstyle :insert-last-word match '*[[:alpha:]/\\]*'
However, you might want to include words that contain spaces:
zstyle :insert-last-word match '*[[:alpha:][:space:]/\\]*'
Or include numbers as long as the word is at least two characters
long:
zstyle :insert-last-word match '*([[:digit:]]?|[[:alpha:]/\\])*'
The above example causes redirections like "2>" to be included.
prompt
The incremental-complete-word widget shows the value of this style
in the status line during incremental completion. The string
value may contain any of the following substrings in the manner of
the PS1 and other prompt parameters:
%c
Replaced by the name of the completer function that generated
the matches (without the leading underscore).
%l
When the list style is set, replaced by `...' if the list of
matches is too long to fit on the screen and with an empty
string otherwise. If the list style is `false' or not set,
`%l' is always removed.
%n
Replaced by the number of matches generated.
%s
Replaced by `-no match-', `-no prefix-', or an empty string
if there is no completion matching the word on the line, if
the matches have no common prefix different from the word on
the line, or if there is such a common prefix, respectively.
%u
Replaced by the unambiguous part of all matches, if there is
any, and if it is different from the word on the line.
Like `break-keys', this uses the `:incremental' context.
stop-keys
This style is used by the incremental-complete-word widget. Its
value is treated similarly to the one for the break-keys style
(and uses the same context: `:incremental'). However, in this
case all keys matching the pattern given as its value will stop
incremental completion and will then execute their usual function.
toggle
This boolean style is used by predict-on and its related widgets in
the context `:predict'. If set to one of the standard `true'
values, predictive typing is automatically toggled off in
situations where it is unlikely to be useful, such as when editing
a multi-line buffer or after moving into the middle of a line and
then deleting a character. The default is to leave prediction
turned on until an explicit call to predict-off.
verbose
This boolean style is used by predict-on and its related widgets in
the context `:predict'. If set to one of the standard `true'
values, these widgets display a message below the prompt when the
predictive state is toggled. This is most useful in combination
with the toggle style. The default does not display these
messages.