User guide / Custom key bindings

Submodes and completion programs

Development reference · View source

A mode outside the current buffer type’s role roots is a submode. Entry records its source mode; entering a role root clears that return point. The status line shows its display name. Escape first cancels any pending key or character input; a later Escape returns to the source mode without changing the selection or running a hook. A binding authored directly on the submode for Escape takes priority over this engine cancellation rule.

An after-each program runs once after a successful complete binding that started and ended in the mode, including after character operands arrive. Leaving the mode skips its hook: inherited a enters Insert without deleting. It shares that binding’s count, register, undo group and execution bounds. It does not run between steps, recursively for its own steps, or for macro wrappers. Replayed bindings run their hooks. Failed, no-op and unbound input skips the hook and leaves the submode active. Prompt and background actions end execution.

modes:
  delete-pending:
    inherits: normal
    motions: extend
    after-each: [cut-selection, {action: mode-enter, arg: normal}]
keys:
  bind:
    normal:
      d: {action: mode-enter, arg: delete-pending, keep-count: true, keep-register: true}
    delete-pending:
      d: select-line
      i: null
      i w: select-inside-word

Here 3d2w deletes six words, dd deletes a line, diw deletes the word under the cursor, d3d deletes three lines, and "add deletes a line into register a. d then Escape changes nothing. i: null removes, in this submode only, the i inherited from Normal: a key that runs an action cannot also begin a longer sequence such as i w (see How bindings are resolved ). The boolean keep-count and keep-register options belong only on mode-enter. They reserve still-pending prefixes for the very next binding, including its hook; later steps in the current binding cannot consume them. The next binding consumes the carried prefixes even when unused. A carried count multiplies a newly typed count; a product above 999999 fails before any action. Escape, failed or unbound input, focus or buffer changes, and reload clear the carry. Without these options, prefixes expire at the binding boundary.