3

In VS Code, I am aware that you can create files containing global user keybindings and settings, and you can have workspace-specific files for keybindings and settings, but is it possible to define settings or keybindings specific to a language mode?

For instance, I want Alt + / to mean FSI: Send Line when I'm in F# mode, but not when I'm in markdown mode or JS mode.

And I want my tabs to be 2 spaces when I'm in Elm mode, but 4 spaces in C# mode.

I know you can define keybindings with a when clause like so:

{
    "key": "alt+/",
    "command": "fsi.SendLine",
    "when": "resourceLangId == fsharp"
}

Is this the only way to achieve something like what I'm after?

It seems like it would make sense to be able to define settings/keybindings for mode X in their own files somewhere. I don't like having language mode behaviour scattered about in big global files like this.

2 Answers 2

5

To customize VS Code settings and keybindings based on the programming language, you can use language identifier, file extension or extension identifier.

For settings you can use language entry (in settings.json):

    "[fsharp]": {
        "editor.suggest.insertMode": "replace"
    }

For keybindings you can use the following when clause contexts (in keybindings.json):

editorLangId True when the editor's associated language Id matches.

    {
        "key": "ctrl+e",
        "command": "workbench.action.files.saveAs",
        "when": "editorLangId == fsharp"
    }

resourceLangId True when the Explorer or editor title language Id matches.

    {
        "key": "ctrl+e",
        "command": "workbench.action.files.saveAs",
        "when": "resourceLangId == fsharp"
    }

resourceExtname True when the Explorer or editor filename extension matches.

    {
        "key": "ctrl+e",
        "command": "workbench.action.files.saveAs",
        "when": "resourceExtname == .fs"
    }

extension True when the extension's ID matches.

    {
        "key": "ctrl+e",
        "command": "workbench.action.files.saveAs",
        "when": "extension == ionide.ionide-fsharp"
    }


note: I did not test all of these clauses, but editorLangId and resourceExtname work for me fine.

1
  • Is it possible to add a token or a scope too? e.g. do something else when on a variable name vs html tag. My case - renaming.
    – Qwerty
    Mar 13 at 4:35
0

Visual Studio Code have stated that at this time (2016-10-19), language mode settings are not supported, but are being considered. (https://twitter.com/code/status/788301380557561857)

There are a couple of issues on the VS Code Github repository requesting variants of this feature.

https://github.com/Microsoft/vscode/issues/13532

https://github.com/Microsoft/vscode/issues/1073

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.