How to Set Multiple Cursors in VSCode at Each Find Result

Viewed 7917

Today I wanted to edit all stops in a SQL INSERT that followed a Regex pattern. I realized that if I could use the multi cursor feature in Visual Studio Code I could make a ton of changes all at one time.

For instance, in the following SQL

INSERT INTO [dbo].[Location] (key, longName, shortName, oldRegion, contactId, email, city, state, zip, isActive)
VALUES
('GEORG','Georgian College','GEORG','1A','1271','globalint@school.edu','Barrie','ON','L4M 3X9','1'),
('LOYAL','Loyalist College','LOYAL','2A','1271','globalint@school.edu','Belleville','ON','K8N 5B9','0')
-- etc.

the contactId and isActive weren't really varchars, and I could find them with a regex of '\d+', but I couldn't figure out how to set the cursors to each spot with keyboard commands or shortcuts. How would you do this?

1 Answers

How to Place Multiple Cursors at the End of All Your RegEx Finds

ALT + ENTER is what you're after (Thanks to Mark).

To find this or similar keybindings open the Keyboard Shortcuts and search for findWidget for commands used with CTRL + F, or searchViewlet for commands used with CTRL + SHIFT + F.

These keybinding are useful for both VSCode and Azure Data Studio and can be customized.

Full Example for Your Case

For your example, you could do this through the following keyboard commands:

  1. CTRL + F (Find)
  2. ALT + R (toggleFindRegex command)
  3. '\d+' (what to look for)
  4. ALT + ENTER (add cursors; editor.action.selectAllMatches command)

-OR-

  1. CTRL + SHIFT + L (add cursors; editor.action.selectHighlights command)
  2. ESC (closeFindWidget command)

Now the following text should have four blinking cursors (at the daggers ), where you can edit to your s content!

INSERT INTO [dbo].[Location] (key, longName, shortName, oldRegion, contactId, email, city, state, zip, isActive)
VALUES
('GEORG','Georgian College','GEORG','1A','1271','globalint@school.edu','Barrie','ON','L4M 3X9','1'),
('LOYAL','Loyalist College','LOYAL','2A','1271','globalint@school.edu','Belleville','ON','K8N 5B9','0')
-- etc.
Related