Skip to main content

commandRegistry

MS Code Extension API


MS Code Extension API / core/extensionAPI/registry/commandRegistry

core/extensionAPI/registry/commandRegistry

Interfaces

CommandMeta

Defined in: core/extensionAPI/registry/commandRegistry.ts:54

Metadata that describes a command. Commands with a title appear in the Command Palette. Commands without title are internal — invokable but not listed.

Example

const meta: CommandMeta = {
id: 'workbench.action.files.save',
title: 'Save File',
category: 'File',
icon: 'save',
shortcut: 'Ctrl+S', // display-only hint; real binding is in defaultKeybindings.ts
};

Properties

category?

optional category?: string

Defined in: core/extensionAPI/registry/commandRegistry.ts:87

Category prefix displayed before the title in the palette. Results in "Category: Title" format.

Example
category: 'File' // → "File: Save File"
category: 'Git' // → "Git: Commit Staged"
category: 'Editor' // → "Editor: Format Document"
icon?

optional icon?: string

Defined in: core/extensionAPI/registry/commandRegistry.ts:97

Codicon icon id used in the palette list and menu items.

Example
icon: 'save'
icon: 'git-commit'
icon: 'refresh'
id

id: string

Defined in: core/extensionAPI/registry/commandRegistry.ts:65

Globally unique dot-separated command identifier. Convention: <scope>.<domain>.<action>

Example
'workbench.action.files.save'
'editor.action.formatDocument'
'git.commit'
'explorer.newFile'
shortcut?

optional shortcut?: string

Defined in: core/extensionAPI/registry/commandRegistry.ts:108

Keyboard shortcut hint — for display only. The actual binding must be registered in defaultKeybindings.ts.

Example
shortcut: 'Ctrl+S'
shortcut: 'Ctrl+Shift+P'
shortcut: 'Ctrl+`'
title?

optional title?: string

Defined in: core/extensionAPI/registry/commandRegistry.ts:76

Human-readable label shown in the Command Palette. Omit to hide the command from the palette (internal/programmatic use only).

Example
title: 'Save File'
title: 'Format Document'
title: 'Checkout to...'

IDisposable

Defined in: core/extensionAPI/registry/commandRegistry.ts:129

Returned by registerCommand. Call dispose() to unregister the command, e.g. when an extension is unloaded or a panel unmounts.

Example

const disposable = commands.registerCommand('my.command', handler);

// Later — clean up:
disposable.dispose();

// Or collect multiple disposables:
const subs: IDisposable[] = [];
subs.push(commands.registerCommand('cmd.a', handlerA));
subs.push(commands.registerCommand('cmd.b', handlerB));
// On teardown:
subs.forEach(d => d.dispose());

Methods

dispose()

dispose(): void

Defined in: core/extensionAPI/registry/commandRegistry.ts:130

Returns

void

Type Aliases

CommandHandler

CommandHandler = (...args) => any

Defined in: core/extensionAPI/registry/commandRegistry.ts:37

Any function that handles a command invocation. Can be sync or async — executeCommand always awaits the return value.

Parameters

args

...any[]

Returns

any

Example

const handler: CommandHandler = () => console.log('hello');
const asyncHandler: CommandHandler = async (filePath: string) => {
await fs.writeFile(filePath, '');
};

Variables

commands

const commands: CommandRegistry

Defined in: core/extensionAPI/registry/commandRegistry.ts:544

The global commands singleton — import this everywhere.

Example

import { commands } from '@/core/extensionAPI/registry/commandRegistry';

// Register
commands.registerCommand('my.action', () => doSomething(), { title: 'My Action', category: 'My Ext' });

// Execute
await commands.executeCommand('my.action');

// Execute with args
await commands.executeCommand('explorer.openFile', '/sdcard/project/index.ts');