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");

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! it helped me refine the gotoSymbol (ctrl+shift+o) command based on category: { "key": "ctrl+o", "command": "workbench.action.quickOpen", "args": "@:", "when": "editorTextFocus" }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.