Link Search Menu Expand Document

BuiltIn

link: https://code.visualstudio.com/api/references/commands

vscode.openFolder

  • params
    • uri - (optional) Uri of the folder or workspace file to open. If not provided, a native dialog will ask the user for the folder
    • newWindow - (optional) Whether to open the folder/workspace in a new window or the same. Defaults to opening in the same window.

Components

Command URIs

  • Commands URIs are links that execute a given command.
  • They can be used as clickable links in hover text, completion item details, or inside of webviews.
  • command URI uses the command scheme followed by the command name
    • eg: command:editor.action.addCommentLine

Enablement

Commands support enablement via an enablement property - its value is a when-clause. Enablement applies to all menus and to registered keybindings.

when clause

Conditional operators

Operator Symbol Example  
Equality == “editorLangId == typescript”  
Inequality != “resourceExtname != .js”  
Or || “isLinux || isWindows”  
And && “textInputFocus && !editorReadonly”  
Matches =~ resourceScheme =~ /^untitled$ ^file$/

Contexts

link: https://code.visualstudio.com/docs/getstarted/keybindings#_contexts

Editor contexts

Context name True When
editorFocus An editor has focus, either the text or a widget.
editorTextFocus The text in an editor has focus (cursor is blinking).
textInputFocus Any editor has focus (regular editor, debug REPL, etc.).
inputFocus Any text input area has focus (editors or text boxes).
editorHasSelection Text is selected in the editor.
editorHasMultipleSelections Multiple regions of text are selected (multiple cursors).
editorReadonly The editor is read only.
editorLangId True when the editor’s associated language Id matches. Example: “editorLangId == typescript”.
isInDiffEditor The active editor is a difference editor.
isInEmbeddedEditor True when the focus is inside an embedded editor.

Other Context

  • terminalFocus

FAQ

How can I find out what command is bound to a specific key?

Keyboard Shortcut editor, you can filter on specific keystrokes to see which commands are bound to which keys. Below you can see that Ctrl+Shift+P is bound to Show All Commands to bring up the Command Palette.

How can I add a key binding for only certain file types?

  • Use the editorLangId context key in your when clause:
{ "key": "shift+alt+a",           "command": "editor.action.blockComment",
                                     "when": "editorTextFocus && editorLangId == csharp" }

Related

Use

execute a command

import * as vscode from 'vscode';

function commentLine() {
  vscode.commands.executeCommand('editor.action.addCommentLine');
}

get result from cmd

import * as vscode from 'vscode';

async function printDefinitionsForActiveEditor() {
  const activeEditor = vscode.window.activeTextEditor;
  if (!activeEditor) {
    return;
  }

  const definitions = await vscode.commands.executeCommand<vscode.Location[]>(
    'vscode.executeDefinitionProvider',
    activeEditor.document.uri,
    activeEditor.selection.active
  );

  for (const definition of definitions) {
    console.log(definition);
  }
}

create new command with arg inside extension.ts

import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
  const command = 'myExtension.sayHello';
  const commandHandler = (name: string = 'world') => {
    console.log(`Hello ${name}!!!`);
  };
  context.subscriptions.push(vscode.commands.registerCommand(command, commandHandler));
}

Copyright © 2020 Thence LLC