'In a VS Code extension, is there a way to set the configuration options for the editor (such as word wrap) programmatically?
For an extension I am working on, our end-users would prefer to have options such as word-wrap on by default. We would like to be able to set this at the language level without the user's direct involvement (just for the languages our extension is active for). I am aware of the per language settings. My goal is to set settings such as the following:
"[xml]": {
"editor.wordWrap": "on",
"editor.tabSize": 4
},
without the user have to do so in their own user settings. Is there a way to do this via the Extension API? I am not seeing any obvious ways to do so.
PS The LanguageConfiguration object does not seem to be relevant for setting things like word-wrap.
Solution 1:[1]
Yes, extensions can contribute default editor settings for a language using configurationDefaults
in the package.json
Here's what the builtin markdown extension contributes for example:
{
"name": "vscode-markdown",
...,
"contributes": {
"configurationDefaults": {
"[markdown]": {
"editor.wordWrap": "on",
"editor.quickSuggestions": false
}
}
}
}
This currently only supports editor.*
language specific settings. We are tracking support for contributing additional language specific settings here
Solution 2:[2]
Now you can change settings programmatically through the WorkspaceConfiguration
API:
const configuration = vscode.workspace.getConfiguration()
const target = vscode.ConfigurationTarget.Workspace // Update the setting locally
const overrideInLanguage = true // Update the setting in the scope of the language
// Set the tab size to 4
configuration.update('editor.tabSize', 4, target, overrideInLanguage)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Matt Bierner |
Solution 2 | McKinley |