Skip to main content
Actions and commands are the primary way to implement executable functionality in VS Code. The Actions framework provides a unified system for defining commands, registering them in menus, and binding them to keybindings.

Overview

VS Code distinguishes between:
  • Commands: Executable functions registered in the CommandsRegistry
  • Actions: Higher-level abstractions that combine commands with UI metadata (menus, keybindings, icons, etc.)
  • Menu Items: Commands displayed in specific menus with context-aware visibility

Command Actions

ICommandAction Interface

The ICommandAction interface defines the structure of a command with its metadata:

Localized Strings

Commands should use ILocalizedString for internationalization:
Example:

Action2: The Modern Action API

Action2 is the recommended way to register actions. It combines command registration, menu items, and keybindings in a single declaration.

Basic Action2 Structure

Action2 with Menus and Keybindings

Action2Options

VS Code defines over 100 menu locations. Here are the most commonly used:

Registering Menu Items

You can register menu items independently from commands:
Menu items are organized into groups with ordering:
Create hierarchical menus using submenus:
Example:

Context Keys and When Clauses

Actions use context keys to control visibility and enablement:

Toggle Actions

Actions can represent toggle states:
Example:

Real-World Example

Here’s a complete example from VS Code’s codebase:

Best Practices

Use Action2: Always prefer Action2 and registerAction2() over the older action registration methods. It’s more concise and maintainable.
Localize strings: Always use ILocalizedString with both value and original properties for all user-facing text.
Context awareness: Use when clauses to show actions only in relevant contexts. This keeps menus clean and improves UX.
Group and order: Use consistent grouping and ordering conventions to keep related actions together.

See Also