LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

zle

Zsh line editor builtin

TLDR

Define a new widget from a shell function
$ zle -N [widget_name] [function_name]
copy
List all user-defined widgets
$ zle -l
copy
List all widgets including builtins
$ zle -la
copy
List widgets as recreatable zle commands
$ zle -lL
copy
Delete a user-defined widget
$ zle -D [widget_name]
copy
Create a widget alias
$ zle -A [old_widget] [new_widget]
copy
Invoke a widget from within a shell function
$ zle [widget_name]
copy
Display a message below the command line
$ zle -M "[message]"
copy

SYNOPSIS

zle [-lLaD] [widget ...]zle -N widget [function]zle -C widget completion-widget functionzle -R [-c] [display-string] [string ...]zle -M stringzle -U stringzle -K keymapzle -F [-Lw] [fd [handler]]zle widget [-n num] [-f flag] [-Nw] [-K keymap] [-- args ...]

DESCRIPTION

zle is a builtin command of zsh that controls the Zsh Line Editor, the interactive command-line editing subsystem. It manages widgets (named editing actions), keymaps, display control, and the edit buffer. When called with no arguments, it returns zero if ZLE is currently active. Full documentation is in the zshzle(1) man page.ZLE activates automatically in interactive zsh sessions. It operates in multiline mode (default) or single-line mode (when the SINGLE_LINE_ZLE option is set). Special parameters like BUFFER, CURSOR, LBUFFER, and RBUFFER allow widget functions to manipulate the edit buffer directly.

PARAMETERS

-N widget [function]

Define a new user-defined widget backed by a shell function. If function is omitted, defaults to the widget name
-C widget completion-widget function
Create a user-defined completion widget inheriting behavior from an existing builtin completion widget
-l [-L | -a]
List user-defined widgets. -L: format as zle commands. -a: include builtins
-D widget ...
Delete named user-defined widgets
-A old-widget new-widget
Create an alias; both names refer to the same widget
-R [-c] [display-string] [string ...]
Redisplay the command line. Optional display-string appears in status line. -c clears completion lists
-M string
Display a message string below the command line that persists after the widget returns
-U string
Push characters onto ZLE's input stack for processing after the current widget finishes
-K keymap
Select the named keymap for subsequent keystrokes
-F [-Lw] [fd [handler]]
Install or remove a handler for a file descriptor. -w: handler is a widget. -L: list handlers
-I
Invalidate the current display before external output
-f flag ...
Set flags on the current widget: yank, yankbefore, kill, vichange, nolast

CONFIGURATION

ZLE is configured primarily through .zshrc using bindkey commands and zle -N widget definitions. The KEYTIMEOUT parameter (default 40, in hundredths of a second) controls how long ZLE waits for multi-character key sequences. Hook widgets like zle-line-init, zle-line-finish, and zle-keymap-select run automatically at specific events.Example custom widget that prepends sudo:

$ prepend-sudo() {
  BUFFER="sudo $BUFFER"
  CURSOR+=5
}
zle -N prepend-sudo
bindkey '^s' prepend-sudo
copy

KEYMAPS

ZLE provides eight built-in keymaps:emacs -- Emacs-style editingviins -- Vi insert modevicmd -- Vi command modeviopp -- Vi operator-pending modevisual -- Vi visual selection modeisearch -- Incremental search modecommand -- Command name reading mode.safe -- Immutable fallback keymapThe main keymap links to either emacs or viins depending on the EDITOR environment variable, or can be set with bindkey -e (emacs) or bindkey -v (vi).

SPECIAL PARAMETERS

When a user-defined widget function runs, these read/write parameters are available:BUFFER -- The entire edit buffer contentsCURSOR -- Cursor position (index into BUFFER)LBUFFER -- Buffer contents left of the cursorRBUFFER -- Buffer contents right of the cursorWIDGET -- Name of the widget being executedLASTWIDGET -- Name of the last widget executedKEYS -- Keys typed to invoke this widgetNUMERIC -- Numeric prefix argument, if anyKEYMAP -- Currently selected keymap

CAVEATS

zle is only available inside zsh and is not a POSIX command or available in bash. Widgets can only be invoked when ZLE is active in interactive mode. Widget shell functions run with stdin redirected to /dev/null. Built-in widgets have a dot-prefixed variant (e.g., .end-of-line) that remains accessible even when the widget is overridden.

HISTORY

ZLE is a fundamental component of zsh, the Z Shell, originally written by Paul Falstad in 1990 at Princeton University. It provides zsh with sophisticated line editing modeled after both Emacs and vi editing modes but is zsh's own independent implementation with a widget-based architecture, distinct from GNU Readline used by bash.

SEE ALSO

zsh(1), bindkey(1)

Copied to clipboard
Kai