Windows – How to record and run multiple find/replace with Sublime Text 2

automationsublime-text-2text-editorswindowswindows 7

How do I record and run multiple find/replace with Sublime Text 2? I tried recording a macro but, as it seems, Sublime won't record find/replace, only text input etc.

Best Answer

Yes, Reg Replace is the way to do it. After installing that package, the way to do it is:

  1. Edit <your Sublime Text Directory>/Packages/RegReplace/reg_replace.sublime-settings (you can access this in ST2 at Preferences > Package Settings > Reg Replace > Settings – Default). Create new entries in the "replacements" object, each one being a single regex find/replace. Note that you have to doubly escape special characters. For example,
    {
    "replacements": {
        "do_something_1": {
        "find": "\\s*\\t\\s*",
        "replace": "\\t",
        "greedy": true,
        "case": false
    },
        "do_something_2": {
        "find": "\\n\\n+",
        "replace": "\\n",
        "greedy": true,
        "case": false
    },
    ...
  1. String those individual replacements together into a command for the Command Palette. To do this, edit the file <your Sublime Text Directory>/RegReplace/Default.sublime-commands (Preferences > Package Settings > Reg Replace > Commands – Default). Add something like this:
[
    {
        "caption": "Reg Replace: My RegEx Macro",
        "command": "reg_replace",
        "args": {
            "replacements": [
                "do_something_1",
                "do_something_2"
            ]
         }
     },
     ...

Then all you have to do is invoke the command palette by hitting Ctrl+Shift+P and browse to your newly created command.

For reference, on my Windows 7 installation, the Sublime Text directory is at C:/Users//AppData/Roaming/Sublime Text 2/

Related Question