VS Code插件推荐与配置

1. 为什么选择VS Code?

Visual Studio Code已成为最受欢迎的代码编辑器,得益于:

  • 轻量快速
  • 强大的插件生态
  • 内置Git支持
  • 跨平台(Windows/Mac/Linux)
  • 完全免费开源

2. 必装插件

2.1 语言支持类

Python

  • Python (Microsoft)
    • 智能代码补全
    • 调试支持
    • Jupyter Notebook支持

C/C++

  • C/C++ (Microsoft)
    • IntelliSense
    • 调试和代码浏览

Markdown

  • Markdown All in One
    • 实时预览
    • 快捷键支持
    • 目录自动生成

2.2 效率提升类

Code Runner

快速运行代码片段,支持多种语言。

// settings.json配置
"code-runner.executorMap": {
    "python": "python -u",
    "c": "gcc $fileName -o $fileNameWithoutExt && ./$fileNameWithoutExt"
}

Path Intellisense

自动补全文件路径。

Bracket Pair Colorizer 2

括号配对着色(VS Code 1.60+已内置)。

Auto Rename Tag

自动重命名配对的HTML/XML标签。

2.3 代码质量类

Pylint / Flake8

Python代码检查工具。

"python.linting.pylintEnabled": true,
"python.linting.flake8Enabled": true

ESLint

JavaScript/TypeScript代码检查。

Prettier

代码格式化工具,支持多种语言。

"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"

2.4 Git相关

GitLens

增强Git功能:

  • 行内显示作者信息
  • 提交历史可视化
  • 文件历史对比

Git Graph

可视化Git分支和提交历史。

2.5 主题与美化

主题推荐

  • One Dark Pro:流行的暗色主题
  • Material Theme:Material Design风格
  • Dracula Official:经典护眼主题

文件图标

  • Material Icon Theme:美观的文件图标

2.6 远程开发

Remote - SSH

通过SSH连接远程服务器开发。

Remote - Containers

在Docker容器中开发。

Remote - WSL

在Windows上使用Linux子系统开发。

2.7 其他实用插件

Live Server

本地Web服务器,支持实时刷新。

TODO Highlight

高亮显示TODO、FIXME等注释。

Better Comments

增强注释显示,支持不同类型的注释着色。

Error Lens

在代码行内显示错误和警告。

3. 推荐配置

3.1 settings.json基础配置

{
  // 编辑器基础设置
  "editor.fontSize": 14,
  "editor.tabSize": 4,
  "editor.wordWrap": "on",
  "editor.minimap.enabled": true,
  "editor.renderWhitespace": "boundary",
  
  // 保存时自动格式化
  "editor.formatOnSave": true,
  
  // 字体设置
  "editor.fontFamily": "Consolas, 'Courier New', monospace",
  "editor.fontLigatures": true,
  
  // 文件设置
  "files.autoSave": "afterDelay",
  "files.autoSaveDelay": 1000,
  "files.encoding": "utf8",
  
  // 终端设置
  "terminal.integrated.fontSize": 13,
  "terminal.integrated.fontFamily": "Consolas",
  
  // Python设置
  "python.linting.enabled": true,
  "python.linting.pylintEnabled": true,
  "python.formatting.provider": "black",
  
  // Git设置
  "git.enableSmartCommit": true,
  "git.confirmSync": false,
  
  // 工作区设置
  "workbench.colorTheme": "One Dark Pro",
  "workbench.iconTheme": "material-icon-theme"
}

3.2 快捷键配置

// keybindings.json
[
  {
    "key": "ctrl+shift+r",
    "command": "code-runner.run"
  },
  {
    "key": "ctrl+shift+f",
    "command": "editor.action.formatDocument"
  }
]

4. 工作区配置

针对特定项目创建.vscode/settings.json

{
  "python.defaultInterpreterPath": "./venv/bin/python",
  "python.linting.pylintArgs": [
    "--disable=C0111"
  ],
  "files.exclude": {
    "**/__pycache__": true,
    "**/*.pyc": true
  }
}

5. 代码片段(Snippets)

创建自定义代码片段:

// python.json
{
  "Python main": {
    "prefix": "pymain",
    "body": [
      "def main():",
      "    $0",
      "",
      "if __name__ == '__main__':",
      "    main()"
    ],
    "description": "Python主函数模板"
  }
}

6. 调试配置

.vscode/launch.json示例:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: 当前文件",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal"
    },
    {
      "name": "C++: g++ build and debug",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "preLaunchTask": "C/C++: g++ build active file"
    }
  ]
}

7. 多光标编辑技巧

  • Ctrl + D:选中下一个相同内容
  • Ctrl + Shift + L:选中所有相同内容
  • Alt + Click:添加光标
  • Ctrl + Alt + ↑/↓:向上/下添加光标

8. 命令面板常用命令

Ctrl+Shift+P打开命令面板:

  • Reload Window:重新加载窗口
  • Format Document:格式化文档
  • Change Language Mode:更改语言模式
  • Preferences: Open Settings (JSON):打开设置文件

9. 工作区管理

9.1 多根工作区

创建.code-workspace文件:

{
  "folders": [
    {
      "path": "project1"
    },
    {
      "path": "project2"
    }
  ],
  "settings": {
    "files.autoSave": "afterDelay"
  }
}

10. 性能优化

10.1 排除不必要的文件

"files.watcherExclude": {
  "**/.git/objects/**": true,
  "**/node_modules/**": true,
  "**/__pycache__/**": true
},
"search.exclude": {
  "**/node_modules": true,
  "**/venv": true
}

10.2 禁用不必要的插件

定期检查并禁用不常用的插件。

11. 协作开发

11.1 Live Share

实时协作编码插件:

  • 共享编辑会话
  • 共享终端和服务器
  • 语音和文本聊天

12. 学习资源

  • 官方文档:code.visualstudio.com/docs
  • 插件市场:marketplace.visualstudio.com
  • 快捷键PDF:官方快捷键速查表
  • YouTube教程:搜索"VS Code tips and tricks"

13. 总结

合理配置VS Code和选择适合的插件可以极大提升开发效率。建议根据自己的需求逐步添加插件,避免安装过多影响性能。


工欲善其事,必先利其器!