Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code extension - programmatically find keybindings

When programming a vscode extension... Is there a programmatic way to find the keybinding for a provided command?

I would like to be able to see if a user has updated the key mapping from default for a command so that the UI can display the up-to-date binding. (and if not, look up the default binding)

Here are the APIs I've looked into so far:

  • vscode.workspace.getConfiguration() - I cannot determine how to access the keybindings.json file / perform a lookup.

  • vscode.extensions.getExtension(name/id) allows access to the package.json, but not the command or keybinding override.

  • vscode.getCommands does not provide access to the keybinding values either...

like image 508
Jon G Avatar asked Nov 18 '16 16:11

Jon G


1 Answers

You can get keybinding values from the keybindings.json file using NodeJS.

keybindings.json path on diferrent systems:

Windows: %APPDATA%\Code\User\keybindings.json
Mac: $HOME/Library/Application Support/Code/User/keybindings.json
Linux: $HOME/.config/Code/User/keybindings.json

To build the path you'll need to get Environment variables using process.env.{variableName}.

For example for MacOS it'll be:

var process = require('process');
//...
var keybindingsPath = process.env.HOME + "/Library/Application Support/Code/User/keybindings.json";

vscode.workspace.openTextDocument(keybindingsPath).then((document) => {
    let text = document.getText();
    //then use this JSON file for your needs
    //...
});
like image 116
Nikita Kunevich Avatar answered Oct 16 '22 13:10

Nikita Kunevich