Is there a way to define different keybinds depending on file extension in VSCode?

1.1k Views Asked by At

I am trying to get something similar to VSCode Python Jupyter Notebook functionality but with C++, and I have succeeded for the most part by using Code Runner extension along with a custom python script to add a preamble to the cpp file before running it. However, I do not know of a way to make the same keybind that launches Jupyter Notebooks (Shift+Enter) working in cpp files but instead use the Code Runner, and so I was wondering if there is a built-in way or an extension that allows adding keybinds which are prioritized under a particular file extension.

It might also be that there is a better way to do what I'm trying to do that I do not know about. Regardless, it'd be helpful to find out about the options I have.

1

There are 1 best solutions below

0
On BEST ANSWER

According to this answer:

  1. Go into your keyboard shortcuts JSON file (ctrl+shift+p and search keyboard shortcuts)
  2. Create a new entry with the keybind you want and the command you want (if the command is through an extension you should check their Github repo for info on the specific commands enacted inside vscode)
  3. For the "when" section you should use the "editorLangId == ..." clause and place the language you want to use where the ... is.

In the end it should look something like this

{
    "key": "[keybind you want]",
    "command": "[command you want]",
    "when": "editorLangId == [lang]"
}