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

How to customize VS Code keyboard shortcuts?

2月18日 18:05

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
  • 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
  • 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

  1. Press Ctrl+K, Ctrl+S
  2. 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 focus
  • editorHasSelection: Text is selected
  • editorReadonly: Editor is read-only
  • resourceExtname == .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

  1. Press first key combination
  2. 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: macOS
  • linux: Linux
  • windows: Windows

Shortcut Conflict Resolution

Viewing Shortcut Conflicts

  1. Open shortcut editor
  2. Enter shortcut combination
  3. 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)
标签:VSCode