Multiline Cursors in VS Code
Greetings, friends! In VS Code, we can use multiple cursors to help speed up our typing. We can use keyboard shortcuts to highlight multiple items and create multiline cursors. Below is a list of some of my favorite keyboard shortcuts to create multiline cursors.
Shortcut (Mac) | Shortcut (Windows) | Description |
---|---|---|
Command + D | Ctrl + D | Add selection to find next match |
Command + Shift + L | Ctrl + Shift + L | Select all matches |
Option + Command + Up Arrow Key | Shift + Alt + Up Arrow Key | Add cursor above current line |
Option + Command + Down Arrow Key | Shift + Alt + Down Arrow Key | Add cursor below current line |
If you you want to highlight multiple items with the same text, but only the next occurring ones, then the first set of keyboard shortcuts can help.
Type the following in a new file in VS Code. You can name the file whatever you want. Then, add the following list to that file.
donuts are
potatoes are
avocados are
pancakes are
waffles are
We will turn the above text into the following using a multiline cursor in VS Code and typing out delicious
only once!
donuts are delicious
potatoes are delicious
avocados are delicious
pancakes are delicious
waffles are delicious
All you have to do is double-click the word, are
, to highlight it and press Command + D
(on Macs) or Ctrl + D
(on Windows) four times. This will create four additional cursors. Press, the right arrow key and type out delicious
and there you go! You just added a word to five lines at once! We can press the Escape
key to remove the additional cursors, so we're back to a single one again.
If you wanted to highlight all instances of are
at the same time, then we could have used the Command + Shift + L
(on Macs) or Ctrl + Shift + L
(on Windows) keyboard shortcuts instead.
Try highlighting are delicious
with a single cursor. Then, press Command + Shift + L
or Ctrl + Shift + L
to highlight all instances of are delicious
. Press backspace a couple times, and then you're back to just a list of food!
donuts
potatoes
avocados
pancakes
waffles
We can also add a cursor above or below the line our cursor is currently on by using the keyboard shortcuts Option + Command + Up/Down Arrow Key
(on Macs) or Shift + Alt + Up/Down Arrow Key
(on Windows).
Multiline cursors are definitely a huge time saver!