Create a Regex Macro in Sublime

To create a Regex macro in Sublime you need to learn JSON. Just follow this tutorial and you will get it.

Install Sublime

You need to have Sublime right? Download from here.

Learn some JSON

You need to be familiar with JSON.

Install Sublime Package Control

You need to have Sublime Package Control installed.

On the top menu Preferences/Package Control. (It will install it by clicking on it the first time).

Install RegReplace

You need to have RegReplace installed.

Use the shortcut Ctrl+Shift+p. This opens a sort of popup menu.

Then type Package Control: Install Package and hit Enter.

Type RegReplace. Then select it.

Restart Sublime.

Learn Python re regex syntax

To build the Macros you must learn the Python re syntax. Which is slightly different from the default regex engine on Sublime.

This is the documentation for Python re.

Learn which files to edit in Sublime

You need to edit 2 files:

  • The JSON file with the Find and Replace pairs
  • The JSON file with the Macro command

JSON file with Find and Replace pairs

Go to the top menu Preferences/Package Settings/RegReplace/Rules-User

Follow this syntax to see what type of string and values you can create.

This is an example I did, when I copy/paste a piece of HTML code into Sublime. It breaks lines at an specific element, removes the first line and replaces the “and” character.

{
"format": "3.0",
"replacements": {
// break into lines
"break_lines": {
"find": "(<li class=\"card card-user)",
"replace": "\\n\\1",
"greedy": true
},
// remove first ul line
"remove_first_line": {
"find": "^<ul class.*\\n",
"replace": "",
"greedy": true
},
// replace & with and
"replace_amp": {
"find": "&",
"replace": "and",
"greedy": true
}

}
}

Each find and replace rule has this:

// break into lines
"break_lines": {
"find": "(<li class=\"card card-user)",
"replace": "\\n\\1",
"greedy": true
},

A // comment. A command name "break_lines". A pair for find. A pair for replace. You can add more string/value pairs as seen in the official documentation.

Each rule is enclosed with open and close curly braces { rule }. Rules are separated by a comma. The last rule doesn’t have a comma.

Save the file.

JSON file with the Macro command

Go to the top menu. Preferences/Package Settings/RegReplace/Commands-User

This JSON file looks like this:

[
{
"caption": "Reg Replace: Clean HTML into something nice",
"command": "reg_replace",
"args": {"replacements": ["break_lines", "remove_first_line", "replace_amp"]}
}
]

Following the previous example. The caption is how you can find the Macro.

Do Ctrl+Shift+p. Start typing the “caption” name and you will see it listed.

Running the Macro

My workflow is that I have a raw HTML file. I open the file or copy/paste it.

Do Ctrl+Shift+p. Find the Macro name. Click on it and it runs.

Building the Macro

There are 2 processes.

One is testing the regex in Sublime. Edit the JSON. Run the Macro. Test the next regex. Edit JSON. Run Macro and so on. While this workflow works. It is kind of tedious having to go back and forth and run the whole Macro from the beginning every time you add something new to the JSON file.

The official documentation recommends another process here. It allows you to edit the regex rules in a Python panel with highlighted syntax.

Related Post

Go to the Previous Directory in Vim I use Vim.Vim is the most productive text editor if you know some vim commandsWhen I open a file and I am done with it. I often want to go...
Export Data From Firebird Database to CSV File Background Some software applications use a database called Firebird. This is an open source database. If the software application is compiled you ca...
Installing Ruby on Ubuntu Installing Ruby on UbuntuAs seen on RVM...Go to the terminal in Ubuntu.Add the public key from RVM$ gpg --keyserver hkp://keys.gnupg.n...
Installing Hipchat on Ubuntu Tested on:Ubuntu 12.04 LTS Thinkpad T400, 2GB RAM Intel Core 2 Duo CPU P8600, 32bitSources:Official hipchat linux instructions Ask U...

Leave a Reply