Run Multiple Find & Replace Commands in Sublime Text

Avatar of Chris Coyier
Chris Coyier on (Updated on )

📣 Freelancers, Developers, and Part-Time Agency Owners: Kickstart Your Own Digital Agency with UACADEMY Launch by UGURUS 📣

I run Find & Replace commands a lot. Sometimes I’m changing a class name. Sometimes I’m looking for a function reference and want to make sure all instances have been accounted for. Sometimes I’m working on an article and and some conversion process requires some global changes. I’ve often wished I could run a single command to run all the Find & Replace commands I normally have to do individually. I finally web searched my way through a solution so figured I’d write it down.

Install the RegReplace Package

There is no way to do this directly within Sublime Text, so we’ll need a little help. RegReplace works very nicely for this.

Package control is typically the easiest way to do this kind of thing:

Edit the User Settings

The settings for this package is where you define all the find & replace commands you want to be able to call. You add new objects to the replacements section in the JSON. It’s fairly self explanitory. Here’s two I added that I’ll want to be running together:

{
  "replacements": {
    "remove_opening_ps": {
      "find": "<p>",
      "replace": "",
      "greedy": true,
      "case": false
    },
    "remove_closing_ps": {
      "find": "</p>",
      "replace": "",
      "greedy": true,
      "case": false
    }
  }
}

Edit the User Commands

The find & replace chunks we set up don’t do anything by themselves, we need to attach them to a command. This chunk of config is an array of commands we can add to. We could call our two new find/replace bits in a single command we create, like this:

[
  {
    "caption": "Reg Replace: Remove All P's",
    "command": "reg_replace",
    "args": {
      "replacements": [
        "remove_opening_ps",
        "remove_closing_ps"
      ]
    }
  },
]

You can name the command (the caption) whatever you want. It then becomes available as a command to run.

Run the new command!

Easy cheesy.