Visual Studio Code is an editor optimized for source code editing, so it has been equipped with many compelling features that allow developers to quickly get their job done. One of the nifty but magical function of the program is multiple cursors.
In this article, we will share with you a few tricks with VSCode multiple cursors which we've used for forever that can level up your productivity at work.
Deselect cursors from multiple selected cursors
You already know how to hold Alt and click at any position that you want to put a cursor on it, but sometimes, you mis-clicked and have to do it all over again.
Actually, VSCode has a built-in way to deselect a cursor from multiple selected cursors. You can click one of the cursors again (while still holding Alt) and it will be removed immediately.
The second way to remove a cursor is to press Ctrl + U to undo the last cursor operation you've just made. Please note that it works only if you invoke the hotkey right after you put the cursor in the wrong position.
Multiple cursors at matching strings
Similar to the way Find function work in every other text editor, VSCode allows you to search through the file you're editing and put a cursor behind any of it.
The first key combination you need to remember is Ctrl + D, which finds the next word occurrence, select it, and put a cursor right next to it.
Alternatively, you can quickly add a cursor to all instances of a word by pressing Ctrl + Shift + L. After that, you can just type the replacement, all instances will be updated instantly.
Deselecting a few multi-line cursors is easy, too. Just click any of the cursors and it will go away. This will prevent you from starting all over again, which is especially helpful when you are selecting a lot of instances.
Multiple cursors at the end of lines
What if you want to put many cursors at the end of lines? Point and click seems too time-consuming? You can leverage VSCode multi-cursor support to do just that.
Suppose we need to put a cursor at the end of every line that includes "TMDB_URL" in the following example. In order to do that, you select TMDB_URL, press Ctrl + Shift + L to select all of them, then press the End key to move all the cursors to the end of line.
Multiple cursors at the beginning of lines
Similar to how we move multiple cursors to the end of line, we can move them to the beginning of lines.
To do that, you select an instance of the word, then press Ctrl + Shift + L to select all of them, finally press the Home key to move all the cursors to the beginning of the lines.
Drag to create multiple cursors
VSCode also allows you to select in "column mode", which is particularly useful when you're working with tabular data. I often call the feature "drag selection" because you would hold a key and drag the mouse to select words.
In order to create multiple cursors using column mode, you place the cursor in one corner and then hold Shift+Alt while dragging to the opposite corner. Everything between the rectangular area created by the mouse would be selected.
Troubleshoot : VSCode multi-cursor not working
Users have reported to us a few cases where VSCode multi-cursor feature does not seem to work for them. There are a few solutions to this specific problem you can try :
- Check VSCode settings in JSON mode and look for
editor.multiCursorModifierkey, you may have unwillingly changed the setting.
- The software you are using is not forwarding the keys/modifier combinations.
- Conflict with other programs. Verify this by closing each program that is running in the background to isolate which program causes the problem. Alternatively, close them all to see whether programs conflict or not.
- Windows users who are working through RDP may have to change the keyboard shortcut. There have been reports that RDP fails to forward full keyboard chords, although no concrete evidence just yet. You can edit keyboard combinations using VSCode settings.