6月2日 00:04
How to customize VS Code keyboard shortcuts?
VS Code provides rich keyboard shortcuts that can greatly improve development efficiency. Through custom shortcut configuration, you can optimize workflows according to personal habits.
Common Shortcuts
Editing Operations
- Ctrl+X: Cut line
- Ctrl+C: Copy line
- Alt+Up/Down Arrow: Move line
- Shift+Alt+Up/Down Arrow: Copy line
- Ctrl+Enter: Insert new line below
- Ctrl+Shift+Enter: Insert new line above
Navigation Operations
- Ctrl+P: Quick open file
- Ctrl+Shift+P: Command palette
- Ctrl+G: Go to line
- Ctrl+T: Go to symbol
- Ctrl+Shift+O: Go to symbol in file
- Ctrl+Tab: Switch editor tabs
Selection Operations
- Ctrl+D: Select next occurrence of word
- Ctrl+Shift+L: Select all occurrences of word
- Alt+Click: Add cursor
- Shift+Alt+Drag: Column selection
Search and Replace
- Ctrl+F: Find
- Ctrl+H: Replace
- Ctrl+Shift+F: Find in files
- Ctrl+Shift+H: Replace in files
Customizing Shortcuts
Shortcut Configuration File
Shortcut configuration is stored in the keybindings.json file.
Opening Shortcut Editor
- Press Ctrl+K, Ctrl+S
- Or through menu: File > Preferences > Keyboard Shortcuts
Custom Shortcut Examples
json[ { "key": "ctrl+shift+;", "command": "editor.action.insertCursorAtEndOfEachLineSelected", "when": "editorTextFocus" }, { "key": "ctrl+alt+/", "command": "editor.action.commentLine", "when": "editorTextFocus" } ]
Conditional Shortcuts
When Clauses
Use when clauses to control shortcut triggering conditions:
json[ { "key": "ctrl+shift+f", "command": "editor.action.formatDocument", "when": "editorHasDocumentFormattingProvider && editorTextFocus && !editorReadonly" } ]
Common When Conditions
editorTextFocus: Editor has focuseditorHasSelection: Text is selectededitorReadonly: Editor is read-onlyresourceExtname == .js: File extension is .js
Multi-key Shortcuts
Defining Multi-key Sequences
json[ { "key": "ctrl+k ctrl+s", "command": "workbench.action.showAllSymbols" } ]
Using Multi-key Shortcuts
- Press first key combination
- Press second key combination after status bar prompt
Platform-specific Shortcuts
Different Platform Configuration
json[ { "key": "ctrl+shift+f", "mac": "cmd+shift+f", "command": "workbench.action.findInFiles" } ]
Platform Identifiers
mac: macOSlinux: Linuxwindows: Windows
Shortcut Conflict Resolution
Viewing Shortcut Conflicts
- Open shortcut editor
- Enter shortcut combination
- View conflicting commands
Disabling Default Shortcuts
json[ { "key": "ctrl+shift+f", "command": "-workbench.action.findInFiles" } ]
Overriding Extension Shortcuts
json[ { "key": "ctrl+shift+f", "command": "myExtension.customCommand", "when": "editorTextFocus" } ]
Shortcut Best Practices
Naming Conventions
- Use meaningful shortcut combinations
- Avoid conflicts with common shortcuts
- Consider cross-platform compatibility
Organizing Shortcuts
- Group by function
- Use consistent naming patterns
- Add comments for explanation
Example Configuration
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" } ]
Important Notes
- Regularly backup shortcut configuration
- Test custom shortcuts work properly
- Consider shortcut consistency for team collaboration
- Avoid over-customization that increases learning cost
- Use shortcut hint feature (Ctrl+K Ctrl+R)