Working with Multiple Cursors in Visual Studio Code

Visual Studio Code has been gaining popularity and has replaced Sublime Text as my editor of choice, and it brought along many of my favorite features of Sublime, including its multiple cursor magic, which is especially great for refactoring. Here are some tips for getting started!

What Is Multi-Cursor Mode?

While multi-cursor mode can be used for column-mode editing, it is much more powerful! It’s column-mode on steroids! Basically, you can place as many cursors in as many places as you want in a single editor view. Once you have your cursors placed, you continue editing, and all operations will be applied to all cursors simultaneously.

Three Ways to Add Cursors

The basic mousey way

The most intuitive way to start adding multiple cursors is with the mouse. Assuming you already have your first cursor placed, you just hold down the Alt key, click where you would like to add another cursor, and then repeat as many times as you’d like. I believe the middle mouse button can be configured so you don’t need to hold down the Alt key.

The column-mode way

If you would like to edit a number of lines at the column position, you can add additional cursors above or below the initial cursor. Just hold down Command+Alt and use the Up and/or Down arrows to add new cursors above or below, respectively.

This is very handy for aligning things in columns since you can skip over white space and words using Ctrl and/or Alt as you would with a single cursor. It’s very handy for editing data files, such as CSV, and it’s also useful when extracting data from log files.

The find-and-replace way

This is possibly my favorite method. If you have a word highlighted, you can select the next occurrence of that word by pressing Command+d, then repeat until you have all instances of the word selected. Alternately, you can add a cursor to all occurrences of the current selection with Command+Shift+L. Then, you just type the replacement, and all instances are updated with each keystroke!

You might end up selecting one or more too many instances of the desired word or phrase. Luckily, Command+u will undo the last multi-cursor operation. This will prevent you from starting allllllll over, which is especially helpful when you are selecting a lot of instances.

While you can use the regular find-and-replace dialog, but I find this much more convenient and intuitive.

Give It a Shot!

Now that you are armed with the basics, go ahead and give it a try. Once you get the hang of this method, you will wonder how you survived without it. You may even blow the minds of some of your Vim or Emacs guru friends or co-workers!

Conversation
  • Kyle says:

    You can select all of the instances with:

    command + control + g

    Perfect if you need to replace a variable.

    • Greg Williams Greg Williams says:

      Thanks Kyle. I actually ran across that yesterday as well. I will add that in. Thanks for the feedback!

  • jorhe vercilo says:

    thats cool!

  • Comments are closed.