VS Code extension command contribution
Add or update VS Code extension commands so package.json contributions expose clear Command Palette actions and correctly hidden Side Bar commands with titles, categories, icons, enablement, and menu placement.
When to invoke
- "Add a command to my VS Code extension."
- "Update command contributions in
package.json." - "Make this command appear in the Command Palette."
- "Add a Side Bar button command with an icon and when clause."
Command contribution rules
VS Code commands must always define a title, independent of category, visibility, or location.
| Command kind | Command ID pattern | Required fields | Visibility rule |
|---|---|---|---|
| Regular command | <extensionId>.<commandName> |
command, title, category |
Accessible in the Command Palette by default. |
| Side Bar command | _extensionId.someCommand#sideBar |
command, title, icon |
Side Bar exclusive commands should not be visible in the Command Palette. |
| View title command | Usually Side Bar pattern | command, title, icon; menu group |
Contribute under view/title with a when clause and an order/position group. |
| View item context command | Usually Side Bar pattern | command, title; menu group |
Contribute under view/item/context with a when clause matching item context. |
Regular command pattern
- Define a user-facing
titlethat starts with a verb. - Define a
categoryso Command Palette entries group under the extension name or feature area. - Omit
iconunless the command will be used in the Side Bar or another UI surface that displays icons. - Keep command IDs stable; changing them breaks keybindings, menus, and user muscle memory.
Side Bar command pattern
- Name Side Bar commands with a leading underscore and
#sideBarsuffix, for example_extensionId.someCommand#sideBar. - Its name follows a special pattern, starting with underscore (
_) and suffixed with#sideBar, like_extensionId.someCommand#sideBarfor instance. Must define anicon, and may or may not have some rule forenablement. - Define an
iconbecause Side Bar buttons often render as icon-only actions. - Add
enablementwhen the command should be disabled instead of hidden. - Add a menu
whencondition so the command is visible only in the intended view or item state. - When contributing to
view/titleorview/item/context, specify an order or position throughgroup, using terms relative to other command/button placement when needed; this is the original order/position rule. - Hide Side Bar exclusive commands from the Command Palette through an appropriate
menus.commandPalettewhencondition.
Examples
Regular Command Palette command
{
"contributes": {
"commands": [
{
"command": "extensionId.refreshData",
"title": "Refresh Data",
"category": "Extension Name"
}
]
}
}
Side Bar exclusive command
{
"contributes": {
"commands": [
{
"command": "_extensionId.refreshView#sideBar",
"title": "Refresh View",
"icon": "$(refresh)"
}
],
"menus": {
"view/title": [
{
"command": "_extensionId.refreshView#sideBar",
"when": "view == extensionId.views.main",
"group": "navigation@10"
}
],
"commandPalette": [
{
"command": "_extensionId.refreshView#sideBar",
"when": "false"
}
]
}
}
}
Gotchas
- Do not omit
title: every command contribution needs one even when the command is hidden or icon-only. - Do not show Side Bar exclusive commands in the Command Palette: hide them explicitly when they only make sense in a view.
- Do not rely on menu position by accident: use
grouporder such asnavigation@10when relative placement matters. - Do not use Side Bar naming for regular commands:
_...#sideBarcommunicates private UI placement.
Output template
## VS Code command contribution result
**Status:** ready | needs manifest context | blocked
**Command kind:** regular | Side Bar | view/title | view/item/context
| Command | Title | Category | Icon | Menu | Visibility |
| --- | --- | --- | --- | --- | --- |
| `<command id>` | `<title>` | `<category or n/a>` | `<icon or n/a>` | `<menu>` | `<Command Palette / Side Bar / hidden>` |
**Manifest changes:** `<package.json contribution summary>`
Quality gate
- Every command defines a
title. - Regular commands define a
categoryand remain accessible in the Command Palette. - Side Bar command IDs start with
_and end with#sideBar, for example_extensionId.someCommand#sideBar. - Side Bar commands define an
icon. - Side Bar exclusive commands are hidden from the Command Palette.
-
view/titleandview/item/contextmenu contributions include an appropriatewhencondition. - Menu placement uses
groupwhen order or relative position matters.