3

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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Do they allow automatic transfer from the find/replace dialog in VSCode, or does one have to copy/paste the expressions?
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?
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.