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
TheICommandAction interface defines the structure of a command with its metadata:
Localized Strings
Commands should useILocalizedString for internationalization:
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
Complete Action2 Example
Complete Action2 Example
Action2Options
Menu System
MenuId Enum
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 Item Structure
Menu Groups and Ordering
Menu items are organized into groups with ordering:Submenus
Create hierarchical menus using submenus:Context Keys and When Clauses
Actions use context keys to control visibility and enablement:Toggle Actions
Actions can represent toggle states: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.