Peter
Peter

Reputation: 3857

In Visual Studio Code is there a way to scope settings and keybindings to a language mode?

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.

Upvotes: 5

Views: 1934

Answers (2)

almaceleste
almaceleste

Reputation: 467

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.

Upvotes: 12

Peter
Peter

Reputation: 3857

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

Upvotes: 0

Related Questions