VS Code 提供丰富的键盘快捷键,可以大幅提高开发效率。通过自定义快捷键配置,可以根据个人习惯优化工作流程。
常用快捷键
编辑操作
- Ctrl+X: 剪切行
- Ctrl+C: 复制行
- Alt+上/下箭头: 移动行
- Shift+Alt+上/下箭头: 复制行
- Ctrl+Enter: 在下方插入新行
- Ctrl+Shift+Enter: 在上方插入新行
导航操作
- Ctrl+P: 快速打开文件
- Ctrl+Shift+P: 命令面板
- Ctrl+G: 跳转到行
- Ctrl+T: 跳转到符号
- Ctrl+Shift+O: 跳转到文件中的符号
- Ctrl+Tab: 切换编辑器标签
选择操作
- Ctrl+D: 选择下一个相同的词
- Ctrl+Shift+L: 选择所有相同的词
- Alt+点击: 添加光标
- Shift+Alt+拖动: 列选择
搜索替换
- Ctrl+F: 查找
- Ctrl+H: 替换
- Ctrl+Shift+F: 在文件中查找
- Ctrl+Shift+H: 在文件中替换
自定义快捷键
快捷键配置文件
快捷键配置存储在 keybindings.json 文件中。
打开快捷键编辑器
- 按 Ctrl+K, Ctrl+S
- 或通过菜单:File > Preferences > Keyboard Shortcuts
自定义快捷键示例
json[ { "key": "ctrl+shift+;", "command": "editor.action.insertCursorAtEndOfEachLineSelected", "when": "editorTextFocus" }, { "key": "ctrl+alt+/", "command": "editor.action.commentLine", "when": "editorTextFocus" } ]
条件快捷键
when 子句
使用 when 子句控制快捷键的触发条件:
json[ { "key": "ctrl+shift+f", "command": "editor.action.formatDocument", "when": "editorHasDocumentFormattingProvider && editorTextFocus && !editorReadonly" } ]
常用 when 条件
editorTextFocus: 编辑器有焦点editorHasSelection: 有选中文本editorReadonly: 编辑器只读resourceExtname == .js: 文件扩展名为 .js
多键快捷键
定义多键序列
json[ { "key": "ctrl+k ctrl+s", "command": "workbench.action.showAllSymbols" } ]
使用多键快捷键
- 按下第一个键组合
- 在状态栏提示后按下第二个键组合
平台特定快捷键
不同平台配置
json[ { "key": "ctrl+shift+f", "mac": "cmd+shift+f", "command": "workbench.action.findInFiles" } ]
平台标识符
mac: macOSlinux: Linuxwindows: Windows
快捷键冲突解决
查看快捷键冲突
- 打开快捷键编辑器
- 输入快捷键组合
- 查看冲突的命令
禁用默认快捷键
json[ { "key": "ctrl+shift+f", "command": "-workbench.action.findInFiles" } ]
覆盖扩展快捷键
json[ { "key": "ctrl+shift+f", "command": "myExtension.customCommand", "when": "editorTextFocus" } ]
快捷键最佳实践
命名规范
- 使用有意义的快捷键组合
- 避免与常用快捷键冲突
- 考虑跨平台兼容性
组织快捷键
- 按功能分组
- 使用一致的命名模式
- 添加注释说明
示例配置
json[ { "key": "ctrl+shift+1", "command": "workbench.action.terminal.new", "when": "!terminalFocus" }, { "key": "ctrl+shift+2", "command": "workbench.action.splitEditor", "when": "!terminalFocus" }, { "key": "ctrl+shift+3", "command": "workbench.action.toggleSidebarVisibility", "when": "!terminalFocus" } ]
注意事项
- 定期备份快捷键配置
- 测试自定义快捷键是否正常工作
- 考虑团队协作时的快捷键一致性
- 避免过度自定义导致学习成本增加
- 使用快捷键提示功能(Ctrl+K Ctrl+R)