Multiline Cursors in VS Code

Published: Saturday, July 9, 2022

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 + DCtrl + DAdd selection to find next match
Command + Shift + LCtrl + Shift + LSelect all matches
Option + Command + Up Arrow KeyShift + Alt + Up Arrow KeyAdd cursor above current line
Option + Command + Down Arrow KeyShift + Alt + Down Arrow KeyAdd 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.

text
Copied! ⭐️
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!

text
Copied! ⭐️
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.

Activating the multiline cursor to add 'delicious' to the end of each line.

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!

text
Copied! ⭐️
donuts
potatoes
avocados
pancakes
waffles

Activating the multiline cursor to remove 'are delicious' from the end of each line.

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!