2

I am writing a vscode extension. I use the following code to enter text in the TextEditor area.

function insertText(params: string) {
  var editor = vscode.window.activeTextEditor;
  editor.edit(edit =>
    editor.selections.forEach(selection => {
      edit.delete(selection);
      edit.insert(selection.start, params);
    })
  );
}

BUT, what I need my extension to be able to enter text in areas like:

  • command palette
  • input area when I press Ctrl+G (for workbench.action.gotoLine command)

instead of asking for user input.


tl;dr

pseudocode for what I am asking:

openCommandPallete();
enterTextInCommandPallete("ABCDEF");
Gama11
  • 31,714
  • 9
  • 78
  • 100
fidgetyphi
  • 928
  • 1
  • 8
  • 17

1 Answers1

4

You can call the quickOpen command with an argument to pre-fill the text:

vscode.commands.executeCommand("workbench.action.quickOpen", "Hello World");

You can switch to the command palette by prefixing the text with >. The full list of possible prefixes for quick open can be checked with ?:

As you can see here, : is the prefix for "Go to Line", so that works with the same command:

vscode.commands.executeCommand("workbench.action.quickOpen", ":5");

There's a related question that deals with how to utilize arguments for quick open in keybindings here.

Gama11
  • 31,714
  • 9
  • 78
  • 100
  • Thanks! it helped me refine the `gotoSymbol` (ctrl+shift+o) command based on category: { "key": "ctrl+o", "command": "workbench.action.quickOpen", "args": "@:", "when": "editorTextFocus" } – VK. Aug 19 '20 at 23:02