乐闻世界logo
搜索文章和话题

VS Code 终端集成功能有哪些?

2月18日 18:15

VS Code 内置强大的终端集成功能,允许在编辑器内直接使用命令行工具,无需切换窗口,极大提升开发效率。

终端基础

打开终端

  • 快捷键: Ctrl+` (反引号)
  • 菜单: View > Terminal
  • 命令面板: Terminal: Create New Terminal

多终端管理

  • 创建新终端: 终端面板右上角的 + 按钮
  • 切换终端: 点击终端标签或使用快捷键
  • 拆分终端: 终端面板右上角的拆分按钮

终端配置

终端类型配置

json
{ "terminal.integrated.defaultProfile.windows": "PowerShell", "terminal.integrated.defaultProfile.osx": "zsh", "terminal.integrated.defaultProfile.linux": "bash" }

终端外观配置

json
{ "terminal.integrated.fontSize": 13, "terminal.integrated.fontFamily": "Menlo, Monaco, 'Courier New'", "terminal.integrated.lineHeight": 1.2, "terminal.integrated.cursorBlinking": true, "terminal.integrated.cursorStyle": "block" }

终端颜色配置

json
{ "workbench.colorCustomizations": { "terminal.ansiBlack": "#000000", "terminal.ansiRed": "#cd3131", "terminal.ansiGreen": "#0dbc79", "terminal.ansiYellow": "#e5e510", "terminal.ansiBlue": "#2472c8", "terminal.ansiMagenta": "#bc3fbc", "terminal.ansiCyan": "#11a8cd", "terminal.ansiWhite": "#e5e5e5" } }

终端操作

常用快捷键

  • Ctrl+`: 切换终端显示/隐藏
  • Ctrl+Shift+`: 创建新终端
  • Ctrl+1/2/3...: 切换到指定终端
  • Ctrl+Shift+1/2/3...: 聚焦到指定终端
  • Ctrl+C: 终止当前命令
  • Ctrl+Shift+C: 复制终端内容
  • Ctrl+Shift+V: 粘贴到终端

终端命令历史

  • 上/下箭头: 浏览命令历史
  • Ctrl+R: 搜索命令历史
  • Ctrl+G: 退出搜索模式

终端滚动

  • Shift+PageUp/PageDown: 快速滚动
  • Ctrl+Home/End: 跳转到开始/结束

终端集成功能

从终端打开文件

bash
# 在终端中执行 code filename.js code .

从终端选择文本

  • 使用鼠标选择文本
  • 自动复制到剪贴板
  • 支持多行选择

终端链接

  • 终端中的文件路径可点击
  • 自动打开对应文件
  • 支持行号跳转

任务集成

从任务运行命令

json
{ "version": "2.0.0", "tasks": [ { "label": "Run Tests", "type": "shell", "command": "npm test", "presentation": { "reveal": "always", "panel": "new" } } ] }

任务配置选项

  • reveal: 何时显示终端(always, silent, never)
  • panel: 使用哪个终端(shared, new, dedicated)
  • focus: 是否聚焦终端
  • clear: 是否清除终端内容

终端环境

环境变量配置

json
{ "terminal.integrated.env.windows": { "PATH": "${env:PATH};C:\\myapp\\bin" }, "terminal.integrated.env.osx": { "PATH": "${env:PATH}:/usr/local/myapp/bin" } }

工作目录配置

json
{ "terminal.integrated.cwd": "${workspaceFolder}" }

高级功能

终端 Shell 集成

  • 自动检测 Shell 类型
  • 支持自定义 Shell 脚本
  • 提供智能提示

终端配置文件

创建自定义终端配置:

json
{ "terminal.integrated.profiles.windows": { "PowerShell": { "source": "PowerShell", "icon": "terminal-powershell" }, "Git Bash": { "path": "C:\\Program Files\\Git\\bin\\bash.exe", "args": ["--login"] } } }

终端 API

在扩展中操作终端:

typescript
const terminal = vscode.window.createTerminal('My Terminal'); terminal.sendText('echo Hello World'); terminal.show();

常见 Shell 配置

PowerShell

json
{ "terminal.integrated.defaultProfile.windows": "PowerShell", "powershell.integrated.consoleTitleTemplate": "PowerShell: ${cwd}" }

Git Bash

json
{ "terminal.integrated.profiles.windows": { "Git Bash": { "path": "C:\\Program Files\\Git\\bin\\bash.exe", "args": ["--login", "-i"] } } }

WSL

json
{ "terminal.integrated.profiles.windows": { "WSL": { "path": "wsl.exe", "args": ["-d", "Ubuntu"] } } }

注意事项

  • 终端会话在关闭 VS Code 时结束
  • 长时间运行的命令可能影响性能
  • 注意终端命令的安全性
  • 合理使用终端历史记录
  • 考虑使用终端扩展增强功能
标签:VSCode