Skip to content

Keymaps & Commands

maxlandon edited this page May 13, 2023 · 11 revisions

This page lists the various commands for the readline library, and gives explanations for its keymap model.

  • If you want to see the list of default binds per keymap, see this page.
  • If you want to learn more about .inputrc files, see this page.

Table of Contents

Click to expand Table of Contents

Keymaps

As explained in the README, the readline library model is strongly influenced by the Z-Shell architecture, which rests on a local (potentially empty) and a main keymap against which to test the keys input by the user.

This model is quite powerful and efficient, as it allows for combinatory use of various line editing commands, with a pretty reliable fallback behavior, as well as a strong integration and support of completion actions, incremental search, history control, etc.

How keymaps work

The shell has a local keymap (empty by default) and a main keymap (never empty):

  • local: can be vi-opp, vi-visual, isearch or menu-select.
  • main: can be emacs-*, vi-insert or vi-command.

Available keymaps

Initially, there are 8 editing keymaps:

  • emacs - EMACS emulation
  • emacs-standard - Alias for emacs
  • emacs-ctlx - Subset of the emacs keymap, containing Ctrl-X-based binds
  • emacs-meta - Subset of the emacs keymap, containing Meta-key-based binds
  • vi-insert - Vim insert mode
  • vi - Vim command/normal mode (aliases vi-move, vi-command)
  • vi-opp - Vim operator pending
  • vi-visual - Vim visual selection

Additionally, two other keymaps:

  • isearch - Incremental search mode, used either for history or tab completion results incremental search (eg. fuzzy search).
  • menu-select - Completion keymap, active when the user asks for completions.

Key dispatch & matching

When a user enters a key, it is first tested against the local keymap. If the key matches one of the binds, and that no other commands in the local keymap match this key by prefix, the widget is executed, and the shell goes back to read another key.

When no widget is matched in the local keymap (or if it's empty), the same process happens in the main keymap. When no keys are matched here either, the key is litteraly inserted in the command line.

The readline library does not make use of timeouts to read keys and match commands against them: when a given key matches one widget bind exactly, but also one or more other commands by prefix, the perfectly matched widget is stored, and the shell reads another key. If the aggregate keys match a widget, it will be executed, and the one saved previously is dropped. Otherwise, if the keys don't match anything, the saved widget is executed with the second key passed as argument to it.

Example

An example is the vaW combination in Vim mode. Here is the process explained step by step:

  • User enters v: the shell enters Visual mode.
  • User enters a: the vi-select-surround widget matches exactly, but select-a-word, select-in-word, etc. also match by prefix: vi-select-surround is cached, and the a key is kept in the stack.
  • User enters ": The aggregate keys are now a". Since no bind exists in Vim operator pending mode for it, the vi-select-surround widget is executed with " as the character to search as a surround element.
  • Alternatively, user enters W: The aggregate keys are aW, which matches select-a-blank-word: this widget is thus executed, and the previously cached vi-select-surround is dropped.

Commands

Conformingly to its inspirations, the following list of commands, descriptions and default keybinds have been directly copied from the Z-Shell ZLE manual page. The non-implemented commands have been removed from the list, new ones have been added, and descriptions adapted where the behavior differs.

The notation is as follows:

  • widget-name (Emacs default key) (Vim Normal key) (Vim Insert key) Description

Movement

  • vi-backward-blank-word (unbound) (B) (unbound) Move backward one word, where a word is defined as a series of non-blank characters.

  • vi-backward-blank-word-end (unbound) (gE) (unbound) Move to the end of the previous word, where a word is defined as a series of non-blank characters.

  • backward-char (^B ESC-[D) (unbound) (unbound) Move backward one character.

  • vi-backward-char (unbound) (^H h ^?) (ESC-[D) Move backward one character, without changing lines.

  • backward-word (ESC-B ESC-b) (unbound) (unbound) Move to the beginning of the previous word.

  • vi-backward-word (unbound) (b) (unbound) Move to the beginning of the previous word, vi-style.

  • vi-backward-word-end (unbound) (ge) (unbound) Move to the end of the previous word, vi-style.

  • beginning-of-line (^A) (unbound) (unbound) Move to the beginning of the line. If already at the beginning of the line, move to the beginning of the previous line, if any.

  • end-of-line (^E) (unbound) (unbound) Move to the end of the line. If already at the end of the line, move to the end of the next line, if any.

  • vi-end-of-line (unbound) ($) (unbound) Move to the end of the line.

  • vi-forward-blank-word (unbound) (W) (unbound) Move forward one word, where a word is defined as a series of non-blank characters.

  • vi-forward-blank-word-end (unbound) (E) (unbound) Move to the end of the current word, or, if at the end of the current word, to the end of the next word, where a word is defined as a series of non-blank characters.

  • forward-char (^F ESC-[C) (unbound) (unbound) Move forward one character.

  • vi-forward-char (unbound) (space l) (ESC-[C) Move forward one character.

  • vi-find-next-char (^X^F) (f) (unbound) Read a character from the keyboard, and move to the next occurrence of it in the line.

  • vi-find-next-char-skip (unbound) (t) (unbound) Read a character from the keyboard, and move to the position just before the next occurrence of it in the line.

  • vi-find-prev-char (unbound) (F) (unbound) Read a character from the keyboard, and move to the previous occurrence of it in the line.

  • vi-find-prev-char-skip (unbound) (T) (unbound) Read a character from the keyboard, and move to the position just after the previous occurrence of it in the line.

  • vi-first-non-blank (unbound) (^) (unbound) Move to the first non-blank character in the line.

  • vi-forward-word (unbound) (w) (unbound) Move forward one word, vi-style.

  • forward-word (ESC-F ESC-f) (unbound) (unbound) Move to the beginning of the next word. The editor’s idea of a word is specified with the WORDCHARS parameter.

  • vi-forward-word-end (unbound) (e) (unbound) Move to the end of the next word.

  • vi-goto-column (ESC-|) (|) (unbound) Move to the column specified by the numeric argument.

  • vi-goto-mark (unbound) (‘) (unbound) Move to the specified mark.


History Control

  • beginning-of-buffer-or-history (ESC-<) (gg) (unbound) Move to the beginning of the buffer, or if already there, move to the first event in the history list.

  • beginning-of-line-hist Move to the beginning of the line. If already at the beginning of the buffer, move to the previous history line.

  • beginning-of-history Move to the first event in the history list.

  • down-line-or-history (^N ESC-[B) (j) (ESC-[B) Move down a line in the buffer, or if already at the bottom line, move to the next event in the history list.

  • vi-down-line-or-history (unbound) (+) (unbound) Move down a line in the buffer, or if already at the bottom line, move to the next event in the history list. Then move to the first non-blank character on the line.

  • down-history (unbound) (^N) (unbound) Move to the next event in the history list.

  • end-of-buffer-or-history (ESC->) (unbound) (unbound) Move to the end of the buffer, or if already there, move to the last event in the history list.

  • end-of-line-hist Move to the end of the line. If already at the end of the buffer, move to the next history line.

  • end-of-history Move to the last event in the history list.

  • history-beginning-search-backward Search backward in the history for a line beginning with the current line up to the cursor. This leaves the cursor in its original position.

  • history-incremental-search-backward (^R ^Xr) (unbound) (unbound) Search backward incrementally for a specified string. The search is case-insensitive if the search string does not have uppercase letters and no numeric argument was given. The string may begin with ‘^’ to anchor the search to the beginning of the line. A restricted set of editing functions is available in the mini-buffer. Keys are looked up in the special isearch keymap, and if not found there in the main keymap (note that by default the isearch keymap is empty). An interrupt signal, as defined by the stty setting, will stop the search and go back to the original line.

    Valid incremental-search commands:

    • accept-and-hold
    • accept-and-infer-next-history
    • accept-line
    • accept-line-and-down-history
    • backward-delete-char
    • vi-backward-delete-char
    • accept-search
    • backward-delete-word
    • backward-kill-word
    • vi-backward-kill-word
    • clear-screen
    • history-incremental-search-backward
    • history-incremental-search-forward
    • vi-cmd-mode
  • history-incremental-search-forward (^S ^Xs) (unbound) (unbound) Search forward incrementally for a specified string. The search is case-insensitive if the search string does not have uppercase letters and no numeric argument was given. The string may begin with ‘^’ to anchor the search to the beginning of the line. The functions available in the mini-buffer are the same as for history-incremental-search-backward.

  • history-search-backward (ESC-P ESC-p) (unbound) (unbound) Search backward in the history for a line beginning with the first word in the buffer.

  • history-search-forward (ESC-N ESC-n) (unbound) (unbound) Search forward in the history for a line beginning with the first word in the buffer.

  • infer-next-history (^X^N) (unbound) (unbound) Search in the history list for a line matching the current one and fetch the event following it.

  • up-line-or-history (^P ESC-[A) (k) (ESC-[A) Move up a line in the buffer, or if already at the top line, move to the previous event in the history list.

  • up-history (unbound) (^P) (unbound) Move to the previous event in the history list.

  • history-beginning-search-forward Search forward in the history for a line beginning with the current line up to the cursor. This leaves the cursor in its original position.

  • history-autosuggest-insert (unbound) (l) (unbound) If historyAutosuggest is enabled in the configuration (fish-style autosuggestions), insert remainder of the history line.


Modifying Text

  • vi-add-eol (unbound) (A) (unbound) Move to the end of the line and enter insert mode.

  • vi-add-next (unbound) (a) (unbound) Enter insert mode after the current cursor position, without changing lines.

  • backward-delete-char (^H ^?) (unbound) (unbound) Delete the character behind the cursor.

  • vi-backward-delete-char (unbound) (X) (^H) Delete the character behind the cursor, without changing lines.

  • backward-kill-line Kill from the beginning of the line to the cursor position.

  • backward-kill-word (^W ESC-^H ESC-^?) (unbound) (unbound) Kill the word behind the cursor.

  • vi-backward-kill-word (unbound) (unbound) (^W) Kill the word behind the cursor, without going past the point where insert mode was last entered.

  • capitalize-word (ESC-C ESC-c) (unbound) (unbound) Capitalize the current word and move past it.

  • vi-change (unbound) (c) (unbound) Read a movement command from the keyboard, and kill from the cursor position to the endpoint of the movement. Then enter insert mode. If the command is vi-change, change the current line.

  • vi-change-eol (unbound) (C) (unbound) Kill to the end of the line and enter insert mode.

  • vi-change-whole-line (unbound) (S) (unbound) Kill the current line and enter insert mode.

  • copy-region-as-kill (ESC-W ESC-w) (unbound) (unbound) Copy the area from the cursor to the mark to the kill buffer.

  • copy-prev-word (ESC-^_) (unbound) (unbound) Duplicate the word to the left of the cursor.

  • copy-prev-shell-word (ESC-[m) (unbound) (unbound) Like copy-prev-word, but the word is found by using shell parsing, whereas copy-prev-word looks for blanks. This makes a difference when the word is quoted and contains spaces.

  • vi-delete (unbound) (d) (unbound) Read a movement command from the keyboard, and kill from the cursor position to the endpoint of the movement. If the command is vi-delete, kill the current line.

  • delete-char Delete the character under the cursor.

  • vi-delete-char (unbound) (x) (unbound) Delete the character under the cursor, without going past the end of the line.

  • delete-word Delete the current word.

  • down-case-word (ESC-L ESC-l) (unbound) (unbound) Convert the current word to all lowercase and move past it.

  • vi-down-case (unbound) (gu) (unbound) Read a movement command from the keyboard, and convert all characters from the cursor position to the endpoint of the movement to lowercase. If the movement command is vi-down-case, swap the case of all characters on the current line.

  • kill-word (ESC-D ESC-d) (unbound) (unbound) Kill the current word.

  • vi-insert (unbound) (i) (unbound) Enter insert mode.

  • vi-insert-bol (unbound) (I) (unbound) Move to the first non-blank character on the line and enter insert mode.

  • kill-line (^K) (unbound) (unbound) Kill from the cursor to the end of the line. If already on the end of the line, kill the newline character.

  • vi-kill-line (unbound) (unbound) (^U) Kill from the cursor back to wherever insert mode was last entered.

  • vi-kill-eol (unbound) (D) (unbound) Kill from the cursor to the end of the line.

  • kill-region (ESC-[w) (unbound) (unbound) Kill from the cursor to the mark.

  • kill-buffer (^X^K) (unbound) (unbound) Kill the entire buffer.

  • kill-whole-line (^U) (unbound) (unbound) Kill the current line.

  • vi-match-bracket (^X^B) (%) (unbound) Move to the bracket character (one of {}, () or []) that matches the one under the cursor. If the cursor is not on a bracket character, move forward without going past the end of the line to find one, and then go to the matching bracket.

  • vi-oper-swap-case (unbound) (g~) (unbound) Read a movement command from the keyboard, and swap the case of all characters from the cursor position to the endpoint of the movement. If the movement command is vi-oper-swap-case, swap the case of all characters on the current line.

  • overwrite-mode (^X^O) (unbound) (unbound) Toggle between overwrite mode and insert mode.

  • vi-put-before (unbound) (P) (unbound) Insert the contents of the kill buffer before the cursor.

  • vi-put-after (unbound) (p) (unbound) Insert the contents of the kill buffer after the cursor.

  • quote-line (ESC-’) (unbound) (unbound) Quote the current line; that is, put a ‘’’ character at the beginning and the end.

  • quote-region (ESC-") (unbound) (unbound) Quote the region from the cursor to the mark.

  • vi-replace (unbound) (R) (unbound) Enter overwrite mode.

  • vi-replace-chars (unbound) (r) (unbound) Replace the character under the cursor with a character read from the keyboard.

  • vi-substitute (unbound) (s) (unbound) Substitute the next character(s).

  • vi-swap-case (unbound) (~) (unbound) Swap the case of the character under the cursor and move past it.

  • transpose-chars (^T) (unbound) (unbound) Exchange the two characters to the left of the cursor if at end of line, else exchange the character under the cursor with the character to the left.

  • transpose-words (ESC-T ESC-t) (unbound) (unbound) Exchange the current word with the one before it.

  • vi-up-case (unbound) (gU) (unbound) Read a movement command from the keyboard, and convert all characters from the cursor position to the endpoint of the movement to lowercase. If the movement command is vi-up-case, swap the case of all characters on the current line.

  • up-case-word (ESC-U ESC-u) (unbound) (unbound) Convert the current word to all caps and move past it.

  • yank (^Y) (unbound) (unbound) Insert the contents of the kill buffer at the cursor position.

  • vi-yank (unbound) (y) (unbound) Read a movement command from the keyboard, and copy the region from the cursor position to the endpoint of the movement into the kill buffer. If the command is vi-yank, copy the current line.

  • vi-yank-whole-line (unbound) (Y) (unbound) Copy the current line into the kill buffer.

  • switch-keyword (^A)|(^X) (^A)|(^X) (unbound) Modifies the current word under the cursor. Currently can only be bound to Ctrl-A and Ctrl-X. If CtrlA, will increment the word, if CtrlX will decrement it. The command supports digits arguments (vi iterations in Vim mode), and will perform the increment n-arg times (can be negative).

    The following word types can be incremented/decremented:

    • Booleans: true|false, t|f, on|off, yes|no, y|n.
    • Operators: &&|||, ++|--, ==|!=, ===| !==, +| -, -| *, *| /, /| +, and| or.
    • Hex digits 0xDe => 0xdf, 0xdE => 0xDF, 0xde0 => 0xddf, 0xffffffffffffffff => 0x0000000000000000.
    • Binary digits: 0b1 => 0b10, 0B0 => 0B1, etc.
    • Integers.

Arguments

  • digit-argument (ESC-0..ESC-9) (1-9) (unbound) Start a new numeric argument, or add to the current one. See also vi-digit-or-beginning-of-line. This only works if bound to a key sequence ending in a decimal digit.

  • neg-argument (ESC--) (unbound) (unbound) Changes the sign of the following argument.


Completion

The widget notation in this section varies slightly: If [M] appears after the widget name, only one bind is shown, because used in the menucomplete local keymap. Otherwise, the classic notation applies: this is where commands actually start a completion action/menu.

  • accept-and-menu-complete [M] In a menu completion, insert the current completion into the buffer, and advance to the next possible completion.

  • accept-completion-or-line [M] In a menu completion, if a candidate is selected, accept it (insert it in the line), and exit the completion menu. If no completion is selected, accept the line. This widget has been added because many people use shell settings where they need to hit Enter to accept the completion, then Enter again to accept the line.

  • complete-word Attempt completion on the current word.

  • delete-char-or-list Delete the character under the cursor. If the cursor is at the end of the line, list possible completions for the current word.

  • expand-or-complete Currently identical to complete-word: expansion is not supported.

  • list-choices (ESC-^D) (unbound) (unbound) List possible completions for the current word.

  • menu-complete (TAB) (unbound) (TAB) Like complete-word, except that menu completion is used.

  • menu-expand-or-complete Like expand-or-complete, except that menu completion is used. Therefore currently identical to menu-complete.

  • menu-incremental-search [M] (^F) In a menu completion (wether a candidate is selected or not), start incremental-search (fuzzy search) on the results. Search backward incrementally for a specified string. The search is case-insensitive if the search string does not have uppercase letters and no numeric argument was given. The string may begin with ‘^’ to anchor the search to the beginning of the line. A restricted set of editing functions is available in the mini-buffer. Keys are looked up in the special isearch keymap, On each change in the mini-buffer, any currently selected candidate is dropped from the line and the menu. An interrupt signal, as defined by the stty setting, will stop the search and go back to the original line.

    Valid incremental-search commands:

    • accept-and-hold
    • accept-and-infer-next-history
    • accept-line
    • accept-line-and-down-history
    • backward-delete-char
    • vi-backward-delete-char
    • accept-search
    • backward-delete-word
    • backward-kill-word
    • vi-backward-kill-word
    • clear-screen
    • history-incremental-search-backward
    • history-incremental-search-forward
    • vi-cmd-mode
  • reverse-menu-complete [M] (^[[Z) Perform menu completion, like menu-complete, except that if a menu completion is already in progress, move to the previous completion rather than the next.

  • menu-complete-next-tag [M] (^[[1;5B) In a menu completion, if there are several tags of completions, go to the first result of the next tag.

  • menu-complete-prev-tag [M] (^[[1;5A) In a menu completion, if there are several tags of completions, go to the first result of the previous tag.

  • vi-registers-complete (^[[Z) (unbound) (^[[Z) Open a completion menu (similar to menu-complete) with all currently populated Vim registers.


Miscellaneous

  • accept-and-hold (ESC-A ESC-a) (unbound) (unbound) Accept the current input line (execute it) and keep it as the buffer on the next readline loop.

  • accept-and-infer-next-history Execute the contents of the buffer. Then search the history list for a line matching the current one and push the event following onto the buffer stack.

  • accept-line (^J ^M) (^J ^M) (^J ^M) Finish editing the buffer. Normally this causes the buffer to be executed as a shell command.

  • accept-line-and-down-history (^O) (unbound) (unbound) Execute the current line, and push the next history event on the buffer stack.

  • vi-cmd-mode (^X^V) (unbound) (^[) Enter command mode; that is, select the ‘vicmd’ keymap. Yes, this is bound by default in emacs mode.

  • clear-screen (^L ESC-^L) (^L) (^L) Clear the screen and redraw the prompt.

  • exchange-point-and-mark (^X^X) (unbound) (unbound) Exchange the cursor position (point) with the position of the mark. Unless a negative numeric argument is given, the region between point and mark is activated so that it can be highlighted. If a zero numeric argument is given, the region is activated but point and mark are not swapped.

  • vi-set-buffer (unbound) (") (unbound) Specify a buffer to be used in the following command. See the registers section in the Vim page.

  • vi-set-mark (unbound) (m) (unbound) Set the specified mark at the cursor position.

  • set-mark-command (^@) (unbound) (unbound) Set the mark at the cursor position. If called with a negative numeric argument, do not set the mark but deactivate the region so that it is no longer highlighted (it is still usable for other purposes). Otherwise the region is marked as active.

  • undo (^_ ^Xu ^X^U) (u) (unbound) Incrementally undo the last text modification. Note that when invoked from vi command mode, the full prior change made in insert mode is reverted, the changes having been merged when command mode was selected.

  • redo (unbound) (^R) (unbound) Incrementally redo undone text modifications.

  • visual-mode (unbound) (v) (unbound) Toggle vim-style visual selection mode.

  • visual-line-mode (unbound) (V) (unbound) Toggle vim-style line-wise visual selection mode.

  • vi-digit-or-beginning-of-line (unbound) (0) (unbound) If the last command executed was a digit as part of an argument, continue the argument. Otherwise, execute vi-beginning-of-line.


Text Objects

Text objects are commands that can be used to select a block of text according to some criteria. They are a feature of the vim text editor and so are primarily intended for use with vi operators or from visual selection mode. However, they can also be used from vi-insert or emacs mode. Key bindings listed below apply to the viopp and visual keymaps.

  • select-a-blank-word (aW) Select a word including adjacent blanks, where a word is defined as a series of non-blank characters.

  • select-a-shell-word (aa) Select the current command argument applying the normal rules for quoting.

  • select-a-word (aw) Select a word including adjacent blanks, using the normal vi-style word definition.

  • select-in-blank-word (iW) Select a word, where a word is defined as a series of non-blank characters.

  • select-in-shell-word (ia) Select the current command argument applying the normal rules for quoting. If the argument begins and ends with matching quote characters, these are not included in the selection.

  • select-in-word (iw) Select a word, using the normal vi-style word definition.

Clone this wiki locally