Visual Studio Code(简称 VS Code)是由微软开发的一款免费、开源的代码编辑器。自 2015 年发布以来,VS Code 已经迅速成为全球最受欢迎的代码编辑器,在 Stack Overflow 开发者调查中连续多年蝉联第一。它支持几乎所有主流编程语言,拥有庞大的扩展市场,并且提供了 IntelliSense 智能补全、内置调试器、集成终端、Git 集成等专业级功能。
VS Code 基于 TypeScript 和 Electron 构建,跨平台支持 Windows、macOS 和 Linux。它的核心优势在于轻量与强大的完美平衡 — 启动速度快如编辑器,功能强大如 IDE。通过丰富的扩展生态,VS Code 可以支持从前端开发、后端编程到数据科学、嵌入式开发等几乎所有开发场景。
| 项目信息 | 详情 |
|---|---|
| 开发团队 | Microsoft |
| GitHub Stars | 168,000+ |
| 官方网站 | code.visualstudio.com |
| 开源协议 | MIT License |
| 技术栈 | TypeScript / Electron |
| 支持平台 | Windows / macOS / Linux / Web |
| 扩展市场 | 50,000+ 扩展 |
| 对比维度 | VS Code | Sublime Text | WebStorm | Vim/Neovim |
|---|---|---|---|---|
| 价格 | 免费开源 | $99 买断(可免费试用) | $69/年 订阅 | 免费开源 |
| 开源 | MIT 开源 | 闭源 | 闭源 | 开源 |
| 启动速度 | 中等(1-3 秒) | 极快(<1 秒) | 较慢(5-10 秒) | 极快(<1 秒) |
| 内存占用 | 中等(300-500 MB) | 低(50-100 MB) | 高(1-2 GB) | 极低(10-50 MB) |
| 插件生态 | 50,000+ 扩展 | 5,000+ 插件 | 内置 + JetBrains 市场 | 丰富但配置复杂 |
| 智能补全 | IntelliSense(强大) | 基础补全 | 开箱即用(最强) | 需配置 LSP |
| 调试功能 | 内置强大调试器 | 无内置调试 | 开箱即用 | 需额外配置 |
| 远程开发 | Remote SSH / Container / WSL | 不支持 | Gateway 远程 | 原生 SSH 支持 |
| 学习曲线 | 简单 | 简单 | 中等 | 陡峭 |
| 适用人群 | 大多数开发者 | 追求极致速度 | 专业 JetBrains 用户 | 终端高手 / 服务器运维 |
# macOS (Homebrew) brew install --cask visual-studio-code # Windows (winget) winget install Microsoft.VisualStudioCode # Ubuntu/Debian sudo apt install code # Arch Linux sudo pacman -S code
code . 命令直接打开项目。Ctrl+Shift+P(macOS: Cmd+Shift+P)打开命令面板,输入 Open User Settings (JSON) 编辑配置文件。以下是推荐的基础配置:
Ctrl+K Ctrl+T(macOS: Cmd+K Cmd+T)
!= => => 等符号更美观
以下 10 个扩展覆盖了代码质量、版本控制、API 测试、远程开发、AI 辅助等核心场景,建议每位开发者都安装。
dbaeumer.vscode-eslint 并安装。
.eslintrc 配置文件。推荐在 settings.json 中添加:
esbenp.prettier-vscode 并安装。
eamodio.gitlens 并安装。
rangav.vscode-thunder-client 并安装。
ms-vscode-remote.remote-ssh 并安装。
F1 输入 Remote-SSH: Connect to Host,输入 user@hostname 即可连接。
ms-azuretools.vscode-docker 并安装。
GitHub.copilot 并安装。需要 GitHub Copilot 订阅($10/月,学生免费)。
Ctrl+I 直接对话
bradlc.vscode-tailwindcss 并安装。
Tailwind CSS: Sort Classes)
usernamehw.errorlens 并安装。
ritwickdey.LiveServer 并安装。
Open with Live Server,或点击底部状态栏的 Go Live 按钮。默认端口 5500。
以下快捷键以 Windows/Linux 为主,macOS 请将 Ctrl 替换为 Cmd,Alt 替换为 Option。
| 快捷键 | 功能 | 说明 |
|---|---|---|
Ctrl+Shift+P | 打开命令面板 | VS Code 最重要的快捷键,几乎所有操作都可以通过它完成 |
Ctrl+P | 快速打开文件 | 输入文件名模糊搜索,按 Enter 打开 |
Ctrl+Shift+E | 聚焦资源管理器 | 快速切换到文件树 |
Ctrl+B | 切换侧边栏 | 隐藏/显示左侧侧边栏 |
Ctrl+\ | 拆分编辑器 | 左右分屏编辑不同文件 |
Ctrl+Tab | 切换已打开的文件 | 在打开的标签页之间快速切换 |
Ctrl+` | 打开/关闭终端 | 集成终端切换 |
| 快捷键 | 功能 | 说明 |
|---|---|---|
Alt+Click | 添加光标 | 在点击位置添加一个新光标 |
Ctrl+Alt+Up/Down | 上下添加光标 | 在上方/下方添加光标,批量编辑多行 |
Ctrl+D | 选择下一个匹配项 | 选中当前单词后,按 Ctrl+D 继续选中下一个相同的单词 |
Ctrl+Shift+L | 选择所有匹配项 | 一次性选中文件中所有相同的单词 |
Alt+Shift+I | 在选中行尾添加光标 | 先选中多行,再按此快捷键在每行末尾添加光标 |
| 快捷键 | 功能 | 说明 |
|---|---|---|
F12 | 跳转到定义 | 跳转到函数/变量的定义位置 |
Alt+F12 | Peek 定义 | 在内联窗口中预览定义,无需跳转 |
Shift+F12 | 查看所有引用 | 查看函数/变量在项目中的所有使用位置 |
Ctrl+Shift+O | 跳转到符号 | 在当前文件中按函数名/类名跳转 |
Ctrl+G | 跳转到行 | 输入行号直接跳转 |
Ctrl+Shift+\ | 跳转到匹配括号 | 在配对的大括号之间跳转 |
Alt+Left/Right | 前进/后退 | 在浏览历史中前进和后退 |
| 快捷键 | 功能 | 说明 |
|---|---|---|
Ctrl+F | 当前文件搜索 | 在当前打开的文件中查找 |
Ctrl+H | 当前文件替换 | 在当前文件中搜索并替换 |
Ctrl+Shift+F | 全局搜索 | 在整个项目中搜索内容 |
Ctrl+Shift+H | 全局替换 | 在整个项目中搜索并替换 |
Alt+Enter | 选中所有匹配结果 | 在搜索框中按 Alt+Enter 选中所有匹配 |
Alt+R | 切换正则表达式 | 在搜索框中启用/关闭正则模式 |
F5 启动调试,首次运行会提示创建 launch.json 配置文件。该文件位于项目的 .vscode/ 目录下。
i > 10),只有条件为真时才暂停。
x = {x}),不暂停程序只打印日志。
user.name — 监视对象属性
arr.length — 监视数组长度
JSON.stringify(data, null, 2) — 格式化 JSON 输出
typeof variable — 查看变量类型
# 以调试模式启动 Node.js node --inspect src/index.js # 启动并等待调试器连接 node --inspect-brk src/index.js然后在 VS Code 中按
F5 选择"Attach to Node Process"。
"sourceMap": true,VS Code 会自动映射 TS 和 JS 文件。
ms-python.python 扩展后即可调试 Python。launch.json 配置:
"justMyCode": false 可以调试第三方库的源代码。
golang.go 扩展后,使用 Delve 调试器调试 Go 程序。launch.json 配置:
VS Code 的远程开发功能是其最具竞争力的特性之一,让你在本地编辑器中无缝操作远程环境。
Remote - SSH 扩展
F1 输入 Remote-SSH: Connect to Host
user@hostname 并回车
my-server 即可一键连接。
Dev Containers 扩展
.devcontainer/devcontainer.json
F1 输入 Dev Containers: Reopen in Container
WSL 扩展
wsl
code . 即可在 VS Code 中打开当前目录
~/projects/),而非 Windows 文件系统(/mnt/c/),这样文件 I/O 性能会快 5-10 倍。Code 按钮,选择 Codespaces 标签
Create codespace on main
GitHub Codespaces 扩展连接
以下整理了 VS Code 使用中最常遇到的 50 个问题及解决方案,按类别分类。
Ctrl+Shift+P 输入 Reload Window 重新加载
code --disable-extensions
Help > Toggle Developer Tools > Console
marketplace.visualstudio.com
"http.proxy": "http://proxy:port"
code --install-extension xxx.vsix
"editor.defaultFormatter": "esbenp.prettier-vscode"2. 为不同语言指定不同的格式化工具:
"[python]": { "editor.defaultFormatter": "ms-python.black-formatter" }
3. 使用 Format Document With... 命令查看可用的格式化工具
"extensions.ignoreRecommendations": true, "extensions.showRecommendationsOnlyOnDemand": true
Ctrl+Shift+P 输入 Process Explorer 查看各进程内存占用
"extensions.autoUpdate": false
npm install eslint --save-dev
.eslintrc.js / eslint.config.js)
eslint-config-prettier 关闭 ESLint 中与 Prettier 冲突的规则:
npm install eslint-config-prettier --save-dev在 ESLint 配置中添加:
{ "extends": ["eslint:recommended", "prettier"] }
Ctrl+Shift+P 输入 Python: Select Interpreter
"python.defaultInterpreterPath": "/usr/bin/python3"
Ctrl+Shift+P 输入 TypeScript: Select TypeScript Version
Use Workspace Version
"typescript.tsdk": "node_modules/typescript/lib"
Ctrl+Shift+P 输入 Startup Performance 查看各阶段耗时
"window.restoreWindows": "none"
files.exclude 排除不需要的目录
Process Explorer(Ctrl+Shift+P > Process Explorer)找出高 CPU 进程
files.watcherExclude 中排除 node_modules 等大目录:
"files.watcherExclude": {
"**/node_modules/**": true,
"**/.git/**": true,
"**/dist/**": true
}
4. 限制 TypeScript 内存:"typescript.tsserver.maxTsServerMemory": 3072
"editor.largeFileOptimizations": true
less、tail)或专用大文件编辑器
--max-memory=8192
"editor.minimap.enabled": false, "editor.renderWhitespace": "none", "editor.renderControlCharacters": false, "editor.suggestOnTriggerCharacters": false2. 禁用动画:
"workbench.list.smoothScrolling": false
"disable-hardware-acceleration": true
search.exclude 中排除不需要搜索的目录:
"search.exclude": {
"**/node_modules": true,
"**/dist": true,
"**/build": true,
"**/.git": true
}
2. 使用 files to include 缩小搜索范围
"search.useRipgrep": true
"security.workspace.trust.enabled": false
"files.autoSave": "afterDelay", "files.autoSaveDelay": 2000或改为焦点变化时保存:
"files.autoSave": "onFocusChange"
Help > Check for Updates
~/.config/Code/Cache(Linux)或 %APPDATA%\Code\Cache(Windows)
code --user-data-dir /tmp/vscode-test 测试是否为配置问题
code --disable-gpu
# 临时增大 sudo sysctl fs.inotify.max_user_watches=524288 # 永久增大 echo "fs.inotify.max_user_watches=524288" | sudo tee -a /etc/sysctl.conf sudo sysctl -p
File > Add Folder to Workspace
"terminal.integrated.fontFamily": "MesloLGS NF"
// Windows 使用 Git Bash
"terminal.integrated.defaultProfile.windows": "Git Bash"
// 或指定自定义路径
"terminal.integrated.profiles.windows": {
"Git Bash": {
"path": "C:\\Program Files\\Git\\bin\\bash.exe"
}
}
"terminal.integrated.profiles.linux": {
"bash": {
"path": "/bin/bash",
"args": ["-l"]
}
}
2. 或在 settings.json 中设置:"terminal.integrated.inheritEnv": true
"terminal.integrated.env.linux": { "TERM": "xterm-256color" }
git config --global color.ui auto
export TERM=xterm-256color
"terminal.integrated.copyOnSelection": true或配置快捷键使 Ctrl+C 在有选中内容时复制,否则发送中断:
"terminal.integrated.sendKeybindingsToShell": false
"terminal.integrated.scrollback": 10000默认值为 1000 行。如果需要保存完整输出,可以使用
Terminal: Export 命令导出终端内容。
Move Terminal into Editor Area
Ctrl+` 最大化终端面板
terminal.integrated.tabs.minWidth
.vscode/tasks.json 配置:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "npm run build",
"group": { "kind": "build", "isDefault": true },
"problemMatcher": ["$tsc"]
}
]
}
确保 command 在终端中可以正常运行,检查工作目录是否正确。
"python.terminal.activateEnvironment": true
"python.condaPath": "/path/to/conda"
source venv/bin/activate(Linux/macOS)或 venv\Scripts\activate(Windows)
"terminal.integrated.gpuAcceleration": "off"
"terminal.integrated.lineHeight": 1.1
Terminal: Create New Terminal
git --version
"git.path": "/usr/bin/git"(或 Windows 上 Git 的安装路径)
git status
"git.enabled": true
git remote set-url origin git@github.com:user/repo.git
git config --global credential.helper store
git config --global credential.helper manager
gh auth login
"files.eol": "\n"
git config --global core.autocrlf input(Linux/macOS)
.gitattributes 文件:
* text=auto eol=lf *.bat text eol=crlf
.gitignore 中
Ctrl+Shift+P > Git: Refresh
git.autoRepositoryDetection 设置
git status 确认
"git.mergeEditor": true
"git.mergeEditor": false
Git: Accept All Current / Accept All Incoming
"gitlens.codeLens.enabled": false, "gitlens.currentLine.enabled": false, "gitlens.hovers.enabled": false或者只保留你需要的功能,禁用其余所有功能。
"git.autofetch": false
git maintenance 优化仓库性能
Git Graph 扩展(mhutchie.git-graph)
Git Graph: View Git Graph 打开可视化分支图
git log --oneline --graph --all
"git.useEditorAsCommitInput": true
Ctrl+Enter 提交(无需手动点击对号按钮)
# 移除追踪但保留本地文件 git rm --cached filename # 移除整个目录 git rm -r --cached directory/ # 重新提交 git commit -m "Remove tracked files from .gitignore"
ssh user@hostname
~/.ssh/config 格式是否正确
rm -rf ~/.vscode-server
"remote.SSH.useLocalServer": false
"remote.SSH.defaultExtensions": [ "dbaeumer.vscode-eslint", "esbenp.prettier-vscode" ]
docker ps
.devcontainer/devcontainer.json 语法是否正确
Dev Containers: Rebuild Container Without Cache
docker system df
wsl
wsl --update
rm -rf ~/.vscode-server
wsl --shutdown 然后重新打开
0.0.0.0 而非 127.0.0.1
"remote.autoForwardPorts": true
ping hostname
~/.ssh/config 中添加 Compression yes
Remote - SSH: Settings 中的 remote.SSH.localServerDownload 优化传输
.devcontainer/devcontainer.json 中的 postCreateCommand 是否耗时过长
~/.ssh/config 中添加保活配置:
Host * ServerAliveInterval 60 ServerAliveCountMax 10 TCPKeepAlive yes同时在远程服务器的
/etc/ssh/sshd_config 中设置:
ClientAliveInterval 60 ClientAliveCountMax 10
rm -rf ~/.vscode-server/bin/旧版本hash
rm -rf ~/.vscode-server/extensions/.cache