VSCode settings.json 配置详解

写给新手的使用指南


什么是 settings.json?

settings.json 是 VSCode 的核心配置文件,用于自定义编辑器的行为、外观、快捷键等功能。

两种配置方式

方式 说明
UI 界面配置 图形化界面,适合新手,但无法配置所有选项
settings.json 纯文本文件,功能全面,是高级配置的必经之路

如何打开 settings.json

  1. Ctrl + Shift + P 打开命令面板
  2. 输入 Open User Settings (JSON) 回车

提示:也可以按 Ctrl + , 打开设置,然后点击右上角的 { } 图标直接打开 JSON 文件。


settings.json 基础结构

{
  // 这是注释
  "section.key": "value",
  "editor.fontSize": 14,
  "workbench.colorTheme": "Default Dark+"
}

基本规则

  • 键名 用引号包裹,冒号 : 分隔键和值
  • 根据类型决定是否加引号:
    • 字符串 → 加引号 "value"
    • 数字 → 不加引号 14
    • 布尔 → 不加引号 true / false
    • 数组 → ["a", "b", "c"]
    • 对象 → { "key": "value" }
  • 最后一个条目不要加逗号

Mermaid 结构图

graph TD A[settings.json] --> B[键值对] B --> C[key: 字符串] B --> D[value: 多种类型] D --> E[字符串 String] D --> F[数字 Number] D --> G[布尔 Boolean] D --> H[数组 Array] D --> I[对象 Object]

常用配置分类

1. 编辑器配置 (editor.*)

{
  // 字体大小
  "editor.fontSize": 16,

  // 字体系列(注意引号中的空格)
  "editor.fontFamily": "Consolas, 'Courier New', monospace",

  // 标签页大小
  "editor.tabSize": 2,

  // 是否使用空格代替 Tab
  "editor.insertSpaces": true,

  // 行列号
  "editor.lineNumbers": "on",

  // 迷你地图
  "editor.minimap.enabled": true,

  // 自动保存
  "files.autoSave": "afterDelay",

  // 拖拽选中文字
  "editor.selectionClipboard": true,

  // 光标样式: block | line | underline
  "editor.cursorStyle": "line",

  // 光标宽度
  "editor.cursorWidth": 2,

  // 单词换行
  "editor.wordWrap": "on"
}

2. 工作台配置 (workbench.*)

{
  // 颜色主题
  "workbench.colorTheme": "Default Dark+",

  // 图标主题
  "workbench.iconTheme": "vs-seti",

  // 启动时显示欢迎页面
  "workbench.startupEditor": "welcomePage",

  // 侧边栏位置: left | right
  "workbench.sideBar.location": "left",

  // 状态栏颜色
  "workbench.colorCustomizations": {
    "statusBar.background": "#1e1e1e"
  }
}

3. 窗口配置 (window.*)

{
  // 窗口标题
  "window.title": "${dirty}${activeEditorShort}${separator}${rootName}${separator}${appName}",

  // 新窗口打开位置: default | newWindow | mainWindow
  "window.openFilesInNewWindow": "default",

  // 还原窗口大小
  "window.restoreWindows": "all",

  // 窗口缩放级别(整数)
  "window.zoomLevel": 0
}

4. 文件配置 (files.*)

{
  // 默认行尾符: \n | \r\n | null
  "files.eol": "\n",

  // 排除的文件(glob 模式)
  "files.exclude": {
    "**/.git": true,
    "**/.DS_Store": true,
    "**/*.pyc": true
  },

  // 文件编码
  "files.encoding": "utf8",

  // 保存时移除尾随空格
  "files.trimTrailingWhitespace": true,

  // 自动猜测编码
  "files.autoGuessEncoding": false
}

5. 格式化配置 (editor.formatOn*)

{
  // 保存时自动格式化
  "editor.formatOnSave": true,

  // 输入时自动格式化(耗性能)
  "editor.formatOnType": false,

  // 粘贴时自动格式化
  "editor.formatOnPaste": false
}

6. 代码片段配置 (editor.snippets.*)

{
  // snippets 扩展语言
  "editor.snippets.preventSuggestions": false,

  // Tab 补全
  "editor.tabCompletion": "onlySnippets"
}

7. 终端配置 (terminal.*)

{
  // Windows 下使用的终端
  "terminal.integrated.shell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",

  // 终端字体大小
  "terminal.integrated.fontSize": 14,

  // 终端光标样式
  "terminal.integrated.cursorStyle": "block",

  // 终端背景透明
  "terminal.integrated.background": "#1e1e1e"
}

8. Git 配置 (git.*)

{
  // 是否启用 Git
  "git.enabled": true,

  // 自动拉取
  "git.autofetch": true,

  // 提交时自动 staging
  "git.autoStash": false,

  // Git 仓库路径
  "git.repository": "default"
}

9. Go 语言配置 (go.*)

{
  // 格式化工具
  "go.formatTool": "gofmt",

  // 保存时格式化
  "go.formatOnSave": true,

  // lint 工具
  "go.lintTool": "golangci-lint",

  // 启用 vet
  "go.vetOnSave": "true"
}

10. Python 配置 (python.*)

{
  // Python 解释器路径
  "python.defaultInterpreterPath": "C:\\Python39\\python.exe",

  // 格式化工具
  "python.formatting.provider": "black",

  // lint 工具
  "python.linting.enabled": true,
  "python.linting.pylintEnabled": true,

  // 保存时格式化
  "python.formatting.onSave": true
}

高级配置

多语言配置 (locale)

{
  // 中文界面
  "locale": "zh-cn"
}

远程开发

{
  // SSH 远程连接配置
  "remote.SSH.showLoginTerminal": true,

  // 远程扩展超时
  "remote.extensionKind": {
    "pub.name": ["workspace"]
  }
}

工作区设置

.vscode/settings.json 中可以设置仅对当前项目生效的配置:

{
  // 仅本项目有效的配置
  "editor.fontSize": 18,
  "terminal.integrated.fontSize": 14
}

多个文件关联

{
  "workbench.editorAssociations": {
    "*.md": "Markdown Preview Enhanced",
    "*.pdf": "vscode.pdf",
    "*.json": "default"
  }
}

自定义快捷键

keybindings.json 中配置:

[
  {
    "key": "ctrl+shift+alt+f",
    "command": "workbench.action.findFiles",
    "when": "filesExplorerFocus"
  },
  {
    "key": "ctrl+d",
    "command": "editor.action.deleteLines",
    "when": "editorTextFocus && !editorReadonly"
  }
]

配置层级关系

graph TD A[配置来源] --> B[用户设置<br/>settings.json] A --> C[工作区设置<br/>.vscode/settings.json] A --> D[远程设置<br/>.vscode-remote] B --> E[优先级: 低] C --> F[优先级: 中] D --> G[优先级: 高] style E fill:#ffcccc style F fill:#ccccff style G fill:#ccffcc

优先级

远程设置 > 工作区设置 > 用户设置 > 默认值

快速查找配置

方法一:UI 搜索

  1. Ctrl + , 打开设置
  2. 搜索框输入关键字
  3. 点击左边的 JSON 图标查看对应代码

方法二:命令面板

  1. Ctrl + Shift + P
  2. 输入 Preferences: Open Settings
  3. 搜索具体配置项

方法三:帮助文档

访问 VSCode 官方文档


常见问题

Q1: 修改后不生效?

  • 检查 JSON 语法是否正确
  • 重启 VSCode 或执行 Reload Window 命令

Q2: 不知道配置项叫什么名字?

  • 在 UI 设置中搜索
  • 查看扩展的文档(如 Python, Go 等插件)

Q3: 如何恢复默认设置?

  • 删除 settings.json 中的条目
  • 或执行 Developer: Reload Window

Q4: 配置项值类型错误?

// ❌ 错误
"editor.fontSize": "16"

// ✅ 正确
"editor.fontSize": 16

配置文件路径

操作系统 路径
Windows %APPDATA%\Code\User\settings.json
macOS ~/Library/Application Support/Code/User/settings.json
Linux ~/.config/Code/User/settings.json

配置示例文件

一个完整的自定义配置示例:

{
  // 编辑器
  "editor.fontSize": 16,
  "editor.fontFamily": "Consolas, 'Courier New', monospace",
  "editor.tabSize": 2,
  "editor.insertSpaces": true,
  "editor.lineNumbers": "on",
  "editor.minimap.enabled": true,
  "editor.formatOnSave": true,
  "editor.wordWrap": "on",
  "editor.cursorStyle": "line",

  // 工作台
  "workbench.colorTheme": "Default Dark+",
  "workbench.iconTheme": "vs-seti",
  "workbench.startupEditor": "welcomePage",
  "workbench.sideBar.location": "left",

  // 文件
  "files.eol": "\n",
  "files.autoSave": "afterDelay",
  "files.trimTrailingWhitespace": true,
  "files.exclude": {
    "**/.git": true,
    "**/.DS_Store": true
  },

  // 终端
  "terminal.integrated.fontSize": 14,
  "terminal.integrated.shell.windows": "powershell.exe",

  // 窗口
  "window.zoomLevel": 0,

  // Git
  "git.enabled": true,
  "git.autofetch": true
}

附录:常用配置速查表

配置项 说明 示例值
editor.fontSize 字号 16
editor.tabSize Tab 宽度 4
editor.insertSpaces 用空格代替 Tab true
editor.wordWrap 自动换行 "on"
files.autoSave 自动保存 "afterDelay"
workbench.colorTheme 颜色主题 "Default Dark+"
git.autofetch 自动获取 true
terminal.integrated.fontSize 终端字号 14
window.zoomLevel 窗口缩放 0
editor.formatOnSave 保存时格式化 true

参考资源


文档更新时间: 2026/05/15

posted @ 2026-07-17 12:02  RK5123153  阅读(334)  评论(0)    收藏  举报