VSCode Keyboard command ids - visual-studio-code

When creating or editing the keybindings.json file v1.2.1 of Visual Studio Code requires not only the key to bind but also the 'CommandID' and 'When' condition.
I am looking for a complete list of 'CommandIDs' available to use and have been unable to find one. Where is that complete list of commands ?
Thanks

Preferences > Open Keyboard Shortcuts.
Click to edit keybindings.json for advanced customization. At the bottom of keybindings.json is a list of available commands:
// Here are other available commands:
This is mentioned (but hard to find) on the page shared by seairth.

Open command palette with Ctrl/Cmd + p and type in Open Default Keyboard Shortcuts(JSON)
then Enter or click on it

Look at Key Bindings for Visual Studio Code for the default list.

This isn't trivial, but does answer the question.
You can retrieve all commands from within a VS Code Extension. So if you create your own like this:
npm install -g yo generator-code
yo code
Then you can use the commands object like this:
let commands = await vscode.commands.getCommands()
Which does the following according to the docs:
Retrieve the list of all available commands. Commands starting with an underscore are treated as internal commands.
See Also: List of all available commands in VSCode

Related

Why is ctrl+~ not working in vscode on linux?

When I press Ctrl+~ to open the terminal in vscode, instead it will show notifications.
How can I find out which program is intercepting this key combination?
Ctrl+~ is actually Ctrl+`
And by default is bound to showing dunst history, check ~/.config/dunst/dunstrc to see the following line is there under the [shortcuts]:
history = ctrl+shift+grave
If that's not it, then the problem is probably you're looking for the ~ (tilde), instead of the ` character.
From: https://forum.endeavouros.com/t/ctrl-does-not-work-on-i3/12645
To answer the question there is a way to troubleshoot keybinding issues. See VSCode Wiki: Keybinding Issues.
In your case you could run the command Developer: Toggle Keyboard Shortcuts Troubleshooting (from the Command Palette) and you presumably would have seen the Ctlr+` resolved to some other command with the source being listed as the extension which overrode the built-in command.

In VS Code, how can I get the path to the currently selected Python?

I want to specify the Python path in tasks.json without an explicit full path. I used to do this using python.pythonPath, but that setting is now deprecated. What is the alternative way to retrieve the currently selected Python path? Are there any other VS Code environment variables?
For the current python path, you need to select an interpreter and the currently used one is shown as a relative path in the box.
In tasks, have args called options so you can define env and PYTHONPATH there
You can find more details about tasks from the Tasks in Visual Studio Code documentation.
If you have the Python VSCode extension installed, and an interpreter is selected, then you can get the path to that interpreter in tasks.json using ${command:python.interpreterPath}.
You can open VS Code command selector with ctrl + shift + p and type "Python: Select Interpreter" to open a dropdown menu of all available interpreters. Doing this should create the appropriate entry to settings.json (which was python.pythonPath for me with version Python plugin version v2021.8.1159798656)
You can also change the interpreter from the bottom toolbar.

How to find out which extension provided the command in vscode?

I'm clueless what feature came out of which extension, is there a way to have its source displayed?
Also would be interesteed to know if its possible to trace the source code of the features.
The only thing I can think of is checking the package.json files, as even vscode.commands.getCommands() only returns plain strings. This can be done with the vscode.extensions API:
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
setTimeout(() => {
for (const extension of vscode.extensions.all) {
let commands = extension.packageJSON.contributes?.commands;
if (!Array.isArray(commands)) {
continue;
}
for (const command of commands) {
console.log(command.title + " is from " + extension.id);
}
}
}, 2000);
}
Note that all only includes activated extensions, hence the timeout to make sure all extensions that activate on startup are done with their activation.
what feature came out of which extension
Yes, you can do this, taking advantage of VSC's intellisense:
I'm Assuming (1) by feature, you mean a command executable from the command pallette and
Open keybindings.json in vscode
You should see JSON like below; if not, add one like below (you're not going to keep it).
{
"key": ".",
"command": "REPLACE THIS WITH YOUR COMMAND NAME",
"when": "suggestWidgetVisible"
}
Where it says "REPLACE THIS WITH YOUR COMMAND NAME", begin typing the name of the command you're interested in
The VSC suggestion widget will open, showing roughly EXTENSION_NAME.YOUR_COMMAND. If the suggest widget doesn't open, press ctrl+space to open it.
Note the name of the extension; that's what contributed that feature/command. If the name doesn't match an extension, it's probably a core VSC feature
trace the source code of the features
Most extensions are on GitHub, as is the core code for VSC, so you can simply navigate to the relevant repository and seacrch the code for that command.
Check this link out from VS Code documentation.
Here you can see where the extensions are installed by default.
And further, you can access their source code.
Where are extensions installed?
Extensions are installed in a per user extensions folder. Depending on your platform, the location is in the following folder:
Windows %USERPROFILE%\.vscode\extensions
macOS ~/.vscode/extensions
Linux ~/.vscode/extensions
You can change the location by launching VS Code with the --extensions-dir command-line option
About which feature cames from which extension, I'm not sure if it's possible to achive that.
Click on an extension and in the editor that opens showing its readme, click on the Feature Contributions link and you'll see the settings and commands that extension contributes. For example:

Add a custom command in Visual Studio Code Command Palette

Is it possible out of the box or using extensions to add a custom command in the Command Palette in Visual Studio Code like "External Tools" as in the IDE from JetBrains or in Visual Studio?
I would like to be able to run custom bash/cmd command directly from the Command Palette.
You can either use VS Code built-in functionality using shortcuts. Just add to keybindings.json:
{
"key": "cmd+shift+R",
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "clear; rails server\u000D"
}
},
Or you can take a look at this extension: Command Runner
https://marketplace.visualstudio.com/items?itemName=usernamehw.commands
This extension can run it from custom Quick Pick (like command palette, but shows only your items). Command id is commands.openAsQuickPick
There's no api to seamlessly add commands to Command Palette #1422, but it's possible to modify package.json what that extension does when this setting is enabled:
"commands.populateCommandPalette": true,
With this setting it will not update Command Palette until the editor is reloaded. It might be an ok experience if you don't do that very often.
You can use multiCommand Extention to build your custom commands, which you can access through the Command Palette. Ctrl+Shift+P > Multi command > custom command.
I know it's not ideal, but I guess you can open multi command with a key binding and then it's almost what you want. Plus the feature that you can execute multiple commands with this extension.
This guy wrote something where you can customize the toolbar. https://github.com/AdamAnandUS/AdamsTool
Maybe add to it with a new StatusBarItem that registers a command you want to run.
https://code.visualstudio.com/docs/extensionAPI/vscode-api#commands.registerCommand
There are also many VS Code Extensions that might do what you want already. https://stackify.com/top-visual-studio-code-extensions/
Go to tools, External tools in visual Studio. Click Add, name the new command then you can point to a batch file command using the browse ellipses. When you save it, you will then see the new menu item under tools.

For VS Code what is the accepted alternative to the deprecated TextEditor.hide() function?

How does one close an editor programmatically in Visual Studio Code? The docs say TextEditor.hide() is deprecated and that we should "Use the command workbench.action.closeActiveEditor instead", but I can't find the workbench.action API.
It's not really an "API", but an identifier for a command in the command palette (see keybindings.json for a list). An extension can invoke commands via executeCommand():
vscode.commands.executeCommand("workbench.action.closeActiveEditor");
This is equivalent to selecting the View: Close Editor command manually or pressing the associated shortcut: