21

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.

3 Answers 3

16

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/

6
  • 1
    I did find a way to edit the Default.sublime-commands from the menu. Preferences -> Package Settings -> Reg Replace
    – Kyle
    Mar 18, 2014 at 15:38
  • Aha. Post updated. May 21, 2014 at 21:02
  • @JohnAlexander, Thanks. Having to figure that out on my own - was as likely to happen as getting my brain surgery degree.
    – mralexgray
    Dec 21, 2015 at 5:53
  • @JohnAlexander if RegReplace get updated automatically by Package Control, will the settings and commands remain?
    – Edward
    Apr 12, 2017 at 5:07
  • @Edward yes, I believe so. Apr 12, 2017 at 15:53
1

You're right Sublime will not record find/replace.

However from this post on Stackoverflow, it appears you can use RegReplace.

0

We don't need Plugins. Here is the sublime text macro code to replace "War" with "Love".

[
    {
        "args": {"to": "bof"}, "command": "move_to"
    },
    {
        "args": {"characters": "War"}, "command": "insert"
    },
    {
        "args": {"extend": true, "to": "bol"}, "command": "move_to"
    },
    {
        "command": "slurp_find_string"
    },
    {
        "args": {"characters": "Love"}, "command": "insert"
    },
    {
        "args": {"extend": true, "to": "bol"}, "command": "move_to"
    },
    {
        "command": "slurp_replace_string"
    },
    {
        "args": null, "command": "left_delete"
    },
    {
        "args": {"close_panel": true }, "command": "replace_all"
    },
]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .