User guide / Custom key bindings

Mode entry and conditional programs

Development reference · View source

mode-enter preserves the selection and changes the active named mode. Its arg names a declared editor mode or one of the buffer type’s normal, select, insert, and replace roles. A missing role is a configuration error. Entering Select initializes the native media selection or captures and focuses terminal review output as appropriate.

modes:
  composing: {inherits: normal}
buffer-types:
  text: {normal: composing, select: select, insert: insert, replace: replace}
keys:
  bind:
    composing:
      F6:
        - {action: mode-enter, arg: insert}
        - insert-newline
      F7:
        if: {mode: select}
        then: [yank]
        else: [select-all, yank]

Both then and else must be lists; either may be empty. A mode condition using a role name tests editing-role membership, including inherited submodes. Other names test the exact active named mode. The condition contracts list every predicate and its applicable buffer types. selection-has-text tests for a nonempty range, not the grapheme a caret would yank. register-empty tests the pending register until consumed, then the unnamed register. {buffer-type: text} tests an exact buffer-type key; like mode guards, it narrows reachable branches during load validation. A tree condition must only be reached in the directory tree. Programs contain at most 16 steps in total and four levels of conditional nesting. A program must contain an action, even when some branches are empty.

{action: mode-enter, arg: select, temporary: true} saves the current mode as the return mode for a temporary selection gesture. Ordinary non-selection actions restore that mode before running; selection operations can continue or consume the gesture. A later explicit mode-enter without temporary: true clears the return mode. Changing pane focus, buffer, terminal session, or buffer type ends the gesture; its return mode never follows you into another buffer type. A condition at the beginning of the next key program restores the previous gesture first; a condition later in a program observes the mode reached by preceding steps.

Configuration checks every reachable step after inheritance and removals have been applied. Each branch contributes its possible resulting modes to the tail. Selection undo/redo and syntax shrink can restore historical modes; insert an explicit mode-enter before a tail that needs one particular mode. Diagnostics identify the buffer type, named mode, key and failing step. A bad binding is rejected independently. Invalid graph changes restore the preset graph while retaining valid key edits. Read-only buffers, terminal state and unavailable language services remain runtime refusals and stop the program.

Action names are the hyphenated names in the reference . An action does not need a default key to be bindable. Any action or colon command can also be named the way the command palette names it, such as theme for open-theme-settings or help for show-help.

Not everything is bindable: internal transitions, retired grammar commands, unsupported actions, and list-only actions are excluded.

A complete example:

keys:
  leader: Ctrl-x
  rebind:
    Space g: Leader G
  bind:
    normal:
      X: extend-line-above
      Alt-x: select-line-up
      F6: [select-all, yank]
      F7: { action: pipe, arg: sort }
      F8: null
    select:
      X: extend-line-above
    insert:
      Ctrl-s: save

Modes are independent. A binding in normal does nothing in select, and insert and replace do not inherit from each other. Repeat a binding in each mode where you want it.

Arguments use the command palette’s own parsing rules.

  • A structured value can also appear inside a list.
  • Commands with a required argument must be given one.
  • Actions do not gain new arguments by being bindable.