一份能直接粘贴的 settings.json

用 Ctrl+Shift+P 打开 "Preferences: Open User Settings (JSON)" 编辑用户级配置。下面这份是所有机器共用的底座:保存时格式化交给 Prettier、ESLint 只修代码不抢格式化、全局搜索排除依赖目录、集成终端指向 PowerShell 7。有个版本坑要注意,editor.codeActionsOnSave 里 source.fixAll.eslint 现在要写 "explicit",老配置里的 true 在新版已经不生效了,很多人以为 ESLint 没工作其实是这里没改。

{
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.tabSize": 4,
  "editor.rulers": [100],
  "editor.minimap.enabled": false,
  "editor.codeActionsOnSave": { "source.fixAll.eslint": "explicit" },
  "files.eol": "
",
  "files.autoSave": "onFocusChange",
  "files.exclude": { "**/.git": true, "**/node_modules": true },
  "search.exclude": { "**/node_modules": true, "**/dist": true, "**/*.min.js": true },
  "terminal.integrated.defaultProfile.windows": "PowerShell",
  "terminal.integrated.fontSize": 13,
  "[php]": { "editor.defaultFormatter": "bmewburn.vscode-intelephense-client" },
  "[python]": { "editor.defaultFormatter": "ms-python.black-formatter" }
}

按场景装扩展,别堆一屏

  • 通用:Error Lens(把报错直接标在行尾)、GitLens、Bookmarks、Todo Tree、EditorConfig for VS Code、Path Intellisense、中文语言包。
  • 前端:ESLint、Prettier、Tailwind CSS IntelliSense、ES7+ 片段。
  • 后端与远程:PHP Intelephense、Python + Pylance、Docker、Remote-SSH、Remote-WSL。

两个取舍值得记:装 Intelephense 后要把内置的 PHP Language Features 建议关掉,否则同一个函数补全弹两遍;用 Remote-SSH 连服务器时扩展装在远端,本地装的不自动生效,得在扩展页点 "Install in SSH"。扩展不是越多越好,每多一个都会拖慢启动和文件监视。

团队统一用工作区配置

个人偏好放用户级,团队约定放仓库里的 .vscode/settings.json 和 .vscode/extensions.json,前者覆盖用户级配置,后者列出推荐扩展,新人克隆后 VS Code 会提示一键安装。再配一份 .editorconfig 管缩进和换行,能在不同编辑器之间保持一致,避免"我这边是 4 空格你那边是 2 空格"的无效 diff。

root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
{
  "recommendations": ["esbenp.prettier-vscode", "dbaeumer.vscode-eslint", "editorconfig.editorconfig"]
}

快捷键与同步的取舍

高频快捷键就这几个:Ctrl+Shift+P 命令面板、Ctrl+P 按文件名跳转、Ctrl+` 切终端、Alt+Shift+F 格式化、F12 跳定义、Ctrl+D 多选相同词、Ctrl+Shift+F 全局搜索、Ctrl+K Ctrl+S 自定义键位。同步方面,内置 Settings Sync 登个账号最省事,但扩展列表和部分设置会一起同步;更可控的是把 settings.json、keybindings.json 和扩展清单提交到自己的 dotfiles 仓库,换机器用脚本还原。

踩坑:多个 formatter 都声明支持同一语言时保存会互相打架,必须显式设 editor.defaultFormatter;formatOnSave 与 ESLint 的 fixAll 顺序不对会来回改,先修代码后格式化;在 WSL 里开发要用 Remote-WSL 重开窗口,否则文件监视和 lint 都不生效。