2

More than often I find that I need some custom regex for me in visual studio code.

Every time I use stackoverflow search to find the same regex rather than trying to rewrite it again and again. Now I am having a separate text note only for that purpose which contains my find/replace regex values. e.g.remove duplicates

Is there a custom smart plugin or option which allows me to add find / replace and give them names and save them (similar to ultra edit) directly in Visual Studio Code?

If there is a way to do the same in Visual Studio instead of code I am fine with it as well - I just need to be able to do find replaces quickly.

Gama11
  • 31,714
  • 9
  • 78
  • 100
user3141326
  • 1,423
  • 2
  • 21
  • 31

1 Answers1

2

There are at least three extensions that can help you:

Find and Transform - which I wrote

replace rules

regreplace

They allow you to store and run (either singly or all on save) a list of regexp's find and replaces.

An example setting (in settings.json) from Find and Replace:

"findInCurrentFile": { 

  "addClassToElement": {
    "title": "Add Class to Html Element",   // will appear in the Command Palette
    "find": ">",
    "replace": " class=\"@\">",
    "restrictFind": "selections",
    "cursorMoveSelect": "@"   // after the replacement, move to and select this text
  }
}

An example keybinding(in keybindings.json) from Find and Replace:

{
  "key": "alt+y",
  "command": "findInCurrentFile",     // note no setting command here
  "args": {
    
    "find": "^([ \\t]*const\\s*)(\\w*)",   // note the double escaping
    
    "replace": "$1\\U$2",                  // capitalize the word following "const"
    "isRegex": true,
    
    "restrictFind": "selections"           // find only in selections
  }
}

So you can save a find/replace or a search across files as a named setting or a keybinding.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • Do they allow automatic transfer from the find/replace dialog in VSCode, or does one have to copy/paste the expressions? – Fuhrmanator Apr 28 '23 at 15:52
  • Do you mean if you already have a find and a replace text in the Find Widget, do you have to copy those values into a setting or keybinding like used above? – Mark Apr 28 '23 at 20:53
  • Yes, I am surprised that VSCode doesn't have this feature of remembering complex regex find/replace (Notepad++ has it for a long time I think). – Fuhrmanator Apr 28 '23 at 21:41