VS Code Multi-cursor Editing and Advanced Selection Techniques
VS Code multi-cursor editing allows editing code at multiple positions simultaneously, greatly improving editing efficiency. Combined with advanced selection techniques, you can quickly complete complex batch editing tasks.
Multi-cursor Creation Methods
Mouse Operations
- Alt + Click: Add cursor at click position
- Ctrl + Alt + Up/Down Arrow: Add cursor above or below
- Ctrl + U: Undo last cursor
Keyboard Operations
- Ctrl + Alt + Up/Down Arrow: Add cursor
- Ctrl + Alt + Left/Right Arrow: Expand selection left or right
Quick Selection
- Ctrl + D: Select next occurrence of current word
- Ctrl + Shift + L: Select all occurrences of current word
- Ctrl + F2: Select all instances of current word
Advanced Selection Techniques
Column Selection Mode
- Shift + Alt + Drag: Column selection
- Ctrl + Shift + Alt + Up/Down Arrow: Column selection expand
Smart Selection
- Shift + Alt + →: Expand selection to next syntax unit
- Shift + Alt + ←: Shrink selection
- Ctrl + Shift + →: Expand selection to next word
- Ctrl + Shift + ←: Shrink selection to previous word
Quick Navigation and Selection
- Ctrl + G: Go to specific line
- Ctrl + T: Go to symbol
- Ctrl + Shift + O: Go to symbol in file
- Ctrl + P: Quick open file
Practical Scenarios
Batch Variable Renaming
javascript// Original code const userName = 'John'; const userAge = 25; const userEmail = 'john@example.com'; // Operation: Double-click userName, press Ctrl + D twice, then modify const firstName = 'John'; const firstAge = 25; const firstEmail = 'john@example.com';
Batch Property Modification
javascript// Original code const obj = { name: 'John', age: 25, email: 'john@example.com' }; // Operation: Select all property names, add quotes const obj = { 'name': 'John', 'age': 25, 'email': 'john@example.com' };
Batch Comment Addition
javascript// Select multiple lines, press Ctrl + / // const line1 = 'code'; // const line2 = 'code'; // const line3 = 'code';
Regex Find and Replace
Using Regular Expressions
- Ctrl + H: Open find and replace
- Alt + R: Enable regex mode
- Ctrl + Alt + Enter: Replace all
Regex Examples
javascript// Find: console\.log\((.*)\) // Replace: console.info($1) // Replace all console.log with console.info
Using Capture Groups
javascript// Find: (\w+)\.(\w+)\((.*)\) // Replace: $2($1, $3) // Convert obj.method(args) to method(obj, args)
Advanced Editing Techniques
Multi-line Editing
- Shift + Enter: Insert new line below current line
- Ctrl + Enter: Insert new line above current line
- Ctrl + Shift + K: Delete current line
Code Formatting
- Shift + Alt + F: Format entire document
- Ctrl + K, Ctrl + F: Format selected portion
Code Movement
- Alt + Up/Down Arrow: Move current line
- Shift + Alt + Up/Down Arrow: Copy current line
Code Folding
- Ctrl + K, Ctrl + 0: Fold all
- Ctrl + K, Ctrl + J: Unfold all
- Ctrl + K, Ctrl + [: Fold current region
- Ctrl + K, Ctrl + ]: Unfold current region
Custom Keyboard Shortcuts
Customize shortcuts in keybindings.json:
json[ { "key": "ctrl+shift+;", "command": "editor.action.insertCursorAtEndOfEachLineSelected", "when": "editorTextFocus" } ]
Important Notes
- Multi-cursor editing may affect performance with many cursors
- Use Ctrl + U to undo cursor operations
- Preview before regex replacement
- Record macros for complex operations
- Use selection history wisely (Ctrl + Shift + G)