31

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?

Eric D. Johnson
  • 10,219
  • 9
  • 39
  • 46
  • 4
    `Alt+Enter` is the easiest way to do this. Enter your find text and the `Alt+Enter` will select them all (no need to change focus from the findwidget). – Mark Sep 15 '20 at 04:00
  • @mark thanks, that's slick. I'll post this shorter version of steps in the answer too. – Eric D. Johnson Sep 15 '20 at 17:30

1 Answers1

72

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.
Eric D. Johnson
  • 10,219
  • 9
  • 39
  • 46
  • 2
    This is amazingly helpful, thanks! – Randy L Mar 04 '22 at 18:21
  • 1
    Thanks so much, This saved me a lot of time trying to select multiple words manually – a3k Mar 22 '22 at 19:42
  • 1
    Is crazy how this little combination has saved me hours of work. Awesome. – Miquel Canal Nov 11 '22 at 20:25
  • 1
    You may also want to check this vscode extension out. Combining with the multiple-cursor, you can generate and do some proessing on the selected text. https://github.com/rioj7/regex-text-gen – Fred Apr 03 '23 at 04:40