为什么选择 VS Code?
Visual Studio Code 是目前最流行的代码编辑器之一,凭借其丰富的扩展生态和出色的性能,已成为开发者日常工作中不可或缺的工具。
必备快捷键
光标与选择
| 快捷键 | 功能 |
|---|---|
Ctrl + D |
选中下一个相同的词 |
Ctrl + Shift + L |
选中所有相同的词 |
Alt + ↑/↓ |
向上/向下移动当前行 |
Ctrl + ←/→ |
按词移动光标 |
Ctrl + Shift + K |
删除当前行 |
多光标编辑
按住 Alt 并点击可以添加多个光标,这是 VS Code 最强大的功能之一。
// 同时修改多个变量名
const firstName = "John";
const lastName = "Doe";
const fullName = firstName + " " + lastName;
// 用多光标可以一次性将 const 全部改为 let
实用扩展推荐
1. GitLens
GitLens 能让你直接在代码中看到每一行的 Git 提交信息,极大地提升了代码审查效率。
{
"gitlens.blame.line.enabled": true,
"gitlens.codeLens.enabled": false,
"gitlens.currentLine.enabled": true
}
2. Prettier
统一的代码格式化工具,团队协作必备。配置如下:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"prettier.singleQuote": true,
"prettier.trailingComma": "es5",
"prettier.tabWidth": 2
}
3. Error Lens
直接在代码行内显示错误和警告信息,无需悬停查看。
自定义配置
{
// 编辑器外观
"editor.fontSize": 14,
"editor.fontFamily": "'JetBrains Mono', 'Fira Code', 'Consolas', monospace",
"editor.fontLigatures": true,
"editor.lineHeight": 24,
"editor.minimap.enabled": false,
"editor.renderWhitespace": "boundary",
"editor.bracketPairColorization.enabled": true,
"editor.guides.bracketPairs": true,
// 终端
"terminal.integrated.fontSize": 13,
"terminal.integrated.cursorBlinking": true,
// 文件
"files.autoSave": "onFocusChange",
"files.exclude": {
"**/__pycache__": true,
"**/.git": true
},
// 搜索
"search.exclude": {
"**/node_modules": true,
"**/dist": true
}
}
工作区设置
对于团队项目,建议使用 .vscode/settings.json 共享工作区配置:
{
"python.linting.enabled": true,
"python.formatting.provider": "black",
"editor.rulers": [88, 120],
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true
}
将这份配置加入版本控制,确保团队成员的开发环境一致。
调试配置
VS Code 的调试功能非常强大,以下是一个 Python Flask 项目的调试配置:
{
"version": "0.2.0",
"configurations": [
{
"name": "Flask Debug",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app/main.py",
"FLASK_ENV": "development",
"FLASK_DEBUG": "1"
},
"args": ["run", "--port=5000"],
"jinja": true
}
]
}
推荐主题
我目前在用的两款主题:
- One Dark Pro — Atom 经典配色,护眼且清晰
- GitHub Theme — 官方出品,风格简洁
切换主题只需按 Ctrl + K Ctrl + T 即可。
小结
VS Code 的强大之处在于可定制性。花时间配置好自己的开发环境,能显著提升日常编码效率。建议每季度 review 一次自己的配置和扩展列表,移除不再需要的,添加新发现的。