Skip to main content

Overview

The vscode.commands namespace provides functions to register and execute commands. Commands are central to how users interact with VS Code, accessible via keyboard shortcuts, menus, actions, and the Command Palette.

Key Functions

registerCommand

Registers a command that can be invoked via keyboard shortcut, menu item, or Command Palette.
function registerCommand(
  command: string,
  callback: (...args: any[]) => any,
  thisArg?: any
): Disposable
command
string
required
A unique identifier for the command (e.g., "myExtension.doSomething")
callback
function
required
The command handler function that executes when the command is invoked
thisArg
any
The this context used when invoking the handler function
Example:
import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
  let disposable = vscode.commands.registerCommand(
    'extension.sayHello',
    () => {
      vscode.window.showInformationMessage('Hello World!');
    }
  );

  context.subscriptions.push(disposable);
}

registerTextEditorCommand

Registers a text editor command with access to the active editor and edit builder.
function registerTextEditorCommand(
  command: string,
  callback: (textEditor: TextEditor, edit: TextEditorEdit, ...args: any[]) => void,
  thisArg?: any
): Disposable
command
string
required
A unique identifier for the command
callback
function
required
Handler with access to TextEditor and TextEditorEdit
Example:
vscode.commands.registerTextEditorCommand(
  'extension.insertText',
  (textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit) => {
    edit.insert(textEditor.selection.start, 'Hello from edit builder!');
  }
);

executeCommand

Executes a command by its identifier.
function executeCommand<T = unknown>(
  command: string,
  ...rest: any[]
): Thenable<T>
command
string
required
Identifier of the command to execute
rest
any[]
Parameters passed to the command function
Example:
// Execute built-in command
await vscode.commands.executeCommand('workbench.action.files.save');

// Execute with arguments
await vscode.commands.executeCommand(
  'vscode.open',
  vscode.Uri.file('/path/to/file')
);
When executing editor commands, only primitive types (string, boolean, number, undefined, null) and VS Code types (Position, Range, Uri, Location) are allowed as arguments.

getCommands

Retrieves the list of all available commands.
function getCommands(filterInternal?: boolean): Thenable<string[]>
filterInternal
boolean
Set true to exclude internal commands (those starting with underscore)
Example:
const allCommands = await vscode.commands.getCommands();
const publicCommands = await vscode.commands.getCommands(true);
console.log(`Total commands: ${allCommands.length}`);

Common Built-in Commands

VS Code provides many built-in commands you can execute:
  • workbench.action.files.save - Save active file
  • workbench.action.files.saveAll - Save all files
  • workbench.action.files.newUntitledFile - Create new file
  • workbench.action.closeActiveEditor - Close active editor
  • editor.action.formatDocument - Format document
  • editor.action.commentLine - Toggle line comment
  • editor.action.selectAll - Select all text
  • editor.action.clipboardCopyAction - Copy to clipboard

Best Practices

Naming Convention

Use reverse domain notation for command IDs: publisher.extension.commandName

Dispose Properly

Always add command disposables to context.subscriptions

Use Text Editor Commands

For editor modifications, use registerTextEditorCommand for automatic edit builder access

Error Handling

Wrap command logic in try-catch to handle errors gracefully

Contribution Points

Learn how to declare commands in package.json

Build docs developers (and LLMs) love