VS Code 的基础配置不需要很长。先确定主题、字体和界面布局,再加入少量能直接改善阅读与编辑的效果;语言工具、远程环境和文档编译留给各自的章节。
打开 Command Palette,运行 Preferences: Open User Settings (JSON),可以直接编辑用户 settings.json。VS Code 使用 JSON with Comments(JSONC),允许注释和尾随逗号。
选择主题和图标
主题控制编辑器与界面配色,图标主题控制 Explorer 中的文件图标。二者是独立设置:
{
"workbench.colorTheme": "Vitesse Dark",
"workbench.iconTheme": "catppuccin-mocha",
"window.autoDetectColorScheme": false
}
这组名称只有安装对应主题扩展后才有效:
code --install-extension antfu.theme-vitesse
code --install-extension catppuccin.catppuccin-vsc-icons
希望 VS Code 跟随系统明暗模式时,将 window.autoDetectColorScheme 改为 true,并分别设置 workbench.preferredLightColorTheme 与 workbench.preferredDarkColorTheme。主题可通过 Preferences: Color Theme 预览,不必反复手改 JSON。VS Code Themes
设置编辑器字体
字体名称按顺序回退。第一种字体没有安装时,VS Code 会尝试下一种:
{
"editor.fontFamily": "'Input Mono', Consolas, monospace",
"editor.fontSize": 16,
"editor.lineHeight": 24,
"editor.fontLigatures": false
}
Input Mono 需要先安装到操作系统。字体名称含空格时加单引号;最后保留 monospace,避免所有候选字体都不可用时退回比例字体。
终端字体单独配置。普通代码字体不一定包含 prompt 使用的图标,使用 Nerd Font 时应只把它指定给终端:
{
"terminal.integrated.fontFamily": "'CaskaydiaCove NF', monospace",
"terminal.integrated.fontSize": 14,
"terminal.integrated.fontWeight": "300"
}
不使用特殊 prompt 图标时,可以省略 terminal.integrated.fontFamily,让终端继承默认等宽字体。VS Code Terminal Appearance
保留有用的编辑反馈
下面几项分别帮助定位当前代码块、看见异常空格和跟踪长文件:
{
"editor.lineNumbers": "interval",
"editor.renderWhitespace": "boundary",
"editor.guides.bracketPairs": "active",
"editor.stickyScroll.enabled": true,
"editor.hover.sticky": true,
"editor.cursorSmoothCaretAnimation": "on",
"workbench.list.smoothScrolling": true
}
lineNumbers: "interval"每隔固定行数显示行号;调试和引用行号较多时可改回"on"。renderWhitespace: "boundary"显示行尾和单词间异常空格,但不会让每个缩进都过于醒目。- bracket pair guides 只突出当前括号范围。
- Sticky Scroll 在长函数中保留当前结构的标题行。
这些都是显示效果,不改变文件内容。
控制标签页和侧边栏
{
"workbench.editor.enablePreview": false,
"workbench.editor.highlightModifiedTabs": true,
"workbench.editor.closeOnFileDelete": true,
"workbench.editor.limit.enabled": true,
"workbench.editor.limit.perEditorGroup": true,
"workbench.editor.limit.value": 5,
"workbench.sideBar.location": "right",
"workbench.tree.expandMode": "singleClick",
"workbench.tree.indent": 10
}
关闭 preview 后,单击文件不会不断替换同一个临时标签。标签上限可以防止编辑器长期堆积几十个文件;固定使用大量并排文件时应适当提高,而不是关闭限制。
侧边栏放左还是右只是布局选择。放在右侧可以避免 Explorer 宽度变化时编辑器左边缘移动。
统一文件的基本格式
{
"files.eol": "\n",
"files.insertFinalNewline": true,
"editor.tabSize": 2,
"diffEditor.ignoreTrimWhitespace": false
}
files.eol 决定新文件默认使用 LF,但团队项目仍应通过 .gitattributes 或 formatter 明确规则。insertFinalNewline 会在保存时补一个文件末尾换行;diffEditor.ignoreTrimWhitespace: false 则让换行和空格变化保持可见。
语言可以覆盖通用缩进:
{
"[python]": {
"editor.tabSize": 4,
"editor.insertSpaces": true
},
"[r]": {
"editor.tabSize": 2,
"editor.insertSpaces": true
}
}
终端使用系统检测结果
在 Windows 中运行 Terminal: Select Default Profile 选择 PowerShell,VS Code 会写入:
{
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.cursorBlinking": true,
"terminal.integrated.cursorStyle": "line",
"terminal.integrated.enablePersistentSessions": false
}
Profile 名称必须与 VS Code 实际检测到的终端一致。自定义终端时可以使用 ${env:APPDATA} 等变量,不必写死用户目录。VS Code Terminal Profiles
不要无缘由设置 terminal.integrated.inheritEnv: false。它会减少终端继承的环境变量,可能让 R、Python、Conda 或编译器突然不在 PATH 中。
一份精简的基础配置
{
"workbench.colorTheme": "Vitesse Dark",
"workbench.iconTheme": "catppuccin-mocha",
"workbench.sideBar.location": "right",
"workbench.editor.enablePreview": false,
"workbench.editor.highlightModifiedTabs": true,
"workbench.editor.limit.enabled": true,
"workbench.editor.limit.perEditorGroup": true,
"workbench.editor.limit.value": 5,
"editor.fontFamily": "'Input Mono', Consolas, monospace",
"editor.fontSize": 16,
"editor.lineHeight": 24,
"editor.tabSize": 2,
"editor.lineNumbers": "interval",
"editor.renderWhitespace": "boundary",
"editor.guides.bracketPairs": "active",
"editor.stickyScroll.enabled": true,
"editor.cursorSmoothCaretAnimation": "on",
"files.eol": "\n",
"files.insertFinalNewline": true,
"diffEditor.ignoreTrimWhitespace": false,
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.cursorBlinking": true,
"terminal.integrated.cursorStyle": "line"
}
这份基础配置没有代理、解释器路径、SSH host、外部 PDF viewer 或特定项目设置,因此可以安全迁移到另一台 Windows 电脑。主题与字体不存在时,先安装对应资源或换成已有选项。