Skip to main content

Reference

The which-key extension aims to provide a keybinding popup similar to emacs-which-key by using the QuickPick API from vscode. This extension was born from VSpaceCode, which was created to emulate the keybinding of spacemacs in VSCode, to decouple the core and the definition of the bindings.

Commands#

Commands are the main way to interact with which-key.

Show#

Command: whichkey.show

Args: string, Array<BindingItem>, undefined

Description: A command to show which-key menu.

  • string is the key of the registered bindings that will be used to display a which-key menu.
  • Array<BindingItem> is an array of BindingItem that will be used directly to display a which-key menu.
  • undefined means the default bindings defined at whichkey.bindings in the configuration will be used to display a which-key menu.

Register#

Command: whichkey.register

Args:

{    bindings: string;    overrides?: string;    title?: string;}

Description: A command to register config which-key, so a canonical record of the bindings lives in memory.

  • bindings is the configuration location of the bindings. e.g. vspacecode.bindings.
  • overrides is the configuration location of the overrides. e.g. vspacecode.overrides.
  • title is the title of the which-key menu to display on the top-level.

Trigger Key#

Command: whichkey.triggerKey

Args: string, { key: string, when?: string }

Description: A command used primarily for accepting non-characters key in which-key menu and pass in when clause via vscode shortcut.

note

Non-character key#

The which-key menu relies on QuickPick's input. We cannot capture non-character keys like arrow keys, the tab key, and keys with modifiers like C-x, because they are not text input. We can capture those keys using vscode shortcuts with whichkeyVisible in the when clause of the shortcuts. See Extra page for more details on usage.

note

Pass-in when clause#

Since vscode doesn't allow an extension to read context key-values (vscode#10471), we have to rely on the when clause evaluation in a vscode shortcut to pass the context to which-key. See conditional bindings for more details on the usage.

Search Bindings#

Command: whichkey.searchBindings

Description: This is similar to helm-descbinds which can search and execute the binding (vscode-which-key#12). Note that you can only search the bindings menu for the currently displayed which-key bindings and their sub-bindings.

Show Transient#

Command: whichkey.showTransient

Args: TransientMenuConfig | string

Description: Show a transient menu with the definition in the argument.

  • an object of TransientMenuConfig
  • a string referencing the location of in the configuration to get TransientMenuConfig

Repeat Recent#

Command: whichkey.repeatRecent

Description: A command to show a which-key menu with keys 1 to 9 to repeat the most recently executed command bindings.

Recent Most Recent#

Command: whichkey.repeatMostRecent

Description: A command to repeat the command binding most recently executed on which-key.

Toggle Zen Mode#

Command: whichkey.toggleZenMode

Description: A command to toggle zen mode for transient menu, which will show/hide all the menu items to save screen real estate.

Open File#

Command: whichkey.openFile

Description: A command to get around an issue where vscode doesn't provide a single cross-platform command to open files. See vscode-which-key#26.

Context#

The extension will also set the following context when applicable to be used in the when clause of vscode shortcuts.

  • whichkeyActive is a boolean that will be set to true when whichkey is active which includes the time during the command execution with the menu being hidden. This is rarely used, and in most of the use cases, it can be replaced with whichKeyVisible.

  • whichkeyVisible is a boolean that will be set to true when the whichkey menu is visible. Note that this will be false when the transient menu is visible.

  • transientVisible is a boolean that will be set to true when the transient menu is visible.

Config#

The extension uses the following configurations.

Delay#

Key: whichkey.delay

Type: number

Default: 0

Description: Delay (in milliseconds) for the which-key menu items to be displayed. Setting this to a positive value will only delay showing the which-key menu items while key inputs remain functional.

Show Icons#

Key: whichkey.showIcons

Type: boolean

Default: true

Description: This option controls whether to show or hide icons in the which-key menu.

Sort Order#

Key: whichkey.sortOrder

Type: "none" | "alphabetically" | "nonNumberFirst"

Default: "none"

Description: This option controls the sorting order of the which-key menu items.

  • "none" will not sort the bindings.
  • "alphabetically" will sort the bindings alphabetically using String.prototype.localeCompare().
  • "nonNumberFirst" will sort the bindings alphabetically like "alphabetically" but have the bindings with number as key last.

Which-key Default Bindings#

Key: whichkey.bindings

Type: Array<BindingItem>

Description: This contains the default which-key menu and is an array of BindingItem.

Transient Bindings#

Key: whichkey.transient

Type: object

Description: A key-value pair to store default definitions of the transient menu for the whichkey.showTransient command to reference.

Shortcuts#

The whichkey extension contributes the following shortcuts, which means they will work without manually editing your user's keybindings.json.

TAB when whichKeyVisible will execute whichkey.triggerKey with \t as argument to which-key in order to capture of tab TAB key.

C-h when whichKeyVisible will execute whichkey.describeBindings, which will show the describe binding menu for the bindings (and its sub bindings) which-key currently displays.

Types#

BindingType#

{    Command = "command",    Commands = "commands",    Bindings = "bindings",    Transient = "transient",    Conditional = "conditional"}
  • Command type indicates a single command will be executed on selection.
  • Commands type indicates multiple commands will be executed on selection.
  • Bindings type indicates that another sub-menu with the supplied bindings will be displayed upon menu item selection.
  • Transient type is similar to the bindings type, except the menu will not disappear on selection. The transient type is being deprecated in favor of the separate command whichkey.showTransient. All current definitions of transient type are converted internally at the moment.
  • Conditional type is an experimental feature that provides conditional binding behavior. See Extra

BindingItem#

{    key: string;    name: string;    icon?: string;    type: BindingType;    bindings?: BindingItem[];    command?: string;    commands?: string[];    args?: any;}
  • key is the key such as k ot \t to trigger the binding.
  • name is the name of the binding such as +buffer or start debug.
  • icon is an optional string property (e.g. rocket) used for the display of vscode product icons as a prefix in the menu item
  • type specifies the type of this binding. see bindingtype
  • bindings is an optional property that is used with binding transient and conditional types.
  • command is an optional property used with command binding type.
  • commands is an optional property used with commands binding type. An array of command strings is expected.
  • args is an optional property that is used to supply arguments to commands for "command" or "commands". When "command" is used, the args will be passed directly to the command being executed. When "commands" is used, an array of the arguments is expected if argument passing is needed. null can be used to indicate no arguments for a specific position if any subsequent command requires an arg.

TransientMenuConfig#

{    title?: string;    bindings: Array<TransientBindingItem>}
  • title is an optional property for the title of the transient menu.
  • bindings is the array of TransientBindingItem.

TransientBindingItem#

{    key: string;    name: string;    exit?: boolean;    command?: string;    commands?: string[];    args?: any;}
  • key is the key such as k ot \t to trigger the binding.
  • name is the name of the binding such as +buffer or start debug. as a prefix in the menu item
  • exit is an optional boolean property. When it is set to true, the transient menu will exit on selection of this item.
  • command is an optional property used with command binding type.
  • commands is an optional property used with commands binding type. An array of command strings is expected.
  • args is an optional property that is used to supply arguments to commands for "command" or "commands". When "command" is used, the args will be passed directly to the command being executed. When "commands" is used, an array of the arguments is expected if argument passing is needed. null can be used to indicate no arguments for a specific position if any subsequent command requires an arg.