乐闻世界logo
搜索文章和话题

How does VS Code settings priority work?

2月18日 18:09

VS Code provides a multi-level settings system that allows configuring editor behavior at different levels. Understanding settings priority is crucial for properly configuring the development environment.

Settings Levels

VS Code supports the following settings levels, in order of priority from high to low:

  1. Workspace Settings - .vscode/settings.json
  2. Remote Settings - Settings in remote development environment
  3. User Settings - User global settings
  4. Default Settings - VS Code default values

Settings File Locations

User Settings

  • macOS: ~/Library/Application Support/Code/User/settings.json
  • Windows: %APPDATA%\Code\User\settings.json
  • Linux: ~/.config/Code/User/settings.json

Workspace Settings

  • .vscode/settings.json in the project root directory

Remote Settings

  • ~/.vscode/data/Machine/settings.json on the remote server

Settings Priority Example

Assume the following settings:

User Settings:

json
{ "editor.fontSize": 14, "editor.tabSize": 2 }

Workspace Settings:

json
{ "editor.fontSize": 16 }

Final Effect:

  • editor.fontSize: 16 (workspace settings override user settings)
  • editor.tabSize: 2 (use user settings)

Language-specific Settings

Settings can be configured for specific languages:

json
{ "editor.fontSize": 14, "[javascript]": { "editor.fontSize": 16, "editor.tabSize": 2 }, "[python]": { "editor.tabSize": 4 } }

Common Settings

Editor Settings

json
{ "editor.fontSize": 14, "editor.tabSize": 2, "editor.insertSpaces": true, "editor.wordWrap": "on", "editor.minimap.enabled": false }

File Associations

json
{ "files.associations": { "*.js": "javascript", "*.jsx": "javascriptreact" } }

Terminal Settings

json
{ "terminal.integrated.fontSize": 13, "terminal.integrated.shell.osx": "/bin/zsh" }

Settings Sync

VS Code provides settings sync functionality to sync across different devices:

  • User settings
  • Keyboard shortcuts
  • Extensions
  • User snippets
  • UI state

Enable method: Settings > Turn on Settings Sync

Dynamic Configuration

Reading and modifying settings in extensions:

typescript
// Read settings const config = vscode.workspace.getConfiguration('editor'); const fontSize = config.get('fontSize', 14); // Listen to settings changes vscode.workspace.onDidChangeConfiguration(event => { if (event.affectsConfiguration('editor.fontSize')) { console.log('Font size changed'); } }); // Modify settings await config.update('fontSize', 16, vscode.ConfigurationTarget.Global);

Important Notes

  • Workspace settings should be committed to version control
  • User settings contain personal preferences and should not be committed
  • Sensitive information (like API keys) should not be stored in settings
  • Pay attention to privacy and security when using Settings Sync
  • For team collaboration, it's recommended to share project configurations in .vscode/settings.json
标签:VSCode