regexvisual-studiovisual-studio-codefind-replace

Visual Studio Code Find/replace - save with name


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.


Solution

  • 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.