Friday, September 13, 2013

Scripting Regular Expressions Find/Replace in Sublime Text

Just like everyone in the software industry, I’ve used many, many different text editors over the years.  vi, Emacs, Notepad, Notepad++, TextPad, nano, gedit… to name a few. But, of course, there is a ridiculously long list of text editors and I haven’t tried that many of them. The one that I’ve been working in for most of this year is Sublime Text. Sublime’s tag line is “Sublime Text: The text editor you’ll fall in love with.” I may not be in love yet, but I’m definitely checking it out a lot.

In addition to having excellent functionality built-in, Sublime also supports a plug-in model. There is the GoSublime plug-in for golang code and there’s the RegReplace plug-in that allows one to write find/replace commands using regular expressions and save them in script as Command Palette commands.

To install RegReplace on Windows:

Step 1: Install Package Control for Sublime Text

As you can see in the installation instructions for Package Control  for Sublime Text, all you have to do is open a Sublime Text console (Ctrl+`) and paste in the install code.

Step 2: Install RegReplace for Sublime Text

image

Option 1: After installing package manager, download RegReplace, unzip it and paste the unzipped folder into your Packages folder (e.g., C:\…\Sublime Text 3\Packages). Next, go to Preferences > Package Control and choose “Package Control: Install Package.” Then choose the package you want to install from the list.

Option 2: This requires you have Git. Open a command prompt. CD to your Sublime Text 3 packages directory, then enter the following command:

git clone -b ST3 https://github.com/facelessuser/RegReplace.git RegReplace

Step 3: Add a Default.sublime-commands file -- with your command added

I had to manually create the folder: C:\Users\username\AppData\Roaming\Sublime Text 3\Packages\RegReplace

Once you have the file, you can add your own custom commands. (Remember to add a comma to the command above.)

   // Test RegReplace
     {
         "caption": "Reg Replace: Test",
         "command": "reg_replace",
         "args": {"replacements": ["test_reg_replace"]}
     },

Step 4: Add your custom command to the Command Palette

To do this, choose Tools > Command Palette > Then type “setting” and you’ll see “Preferences: Reg Replace Settings – User.” Choose this option and paste the example one that comes with RegReplace into the new file that’s created.

This will create the file: /C/Users/username/AppData/Roaming/Sublime Text 3/Packages/RegReplace/reg_replace.sublime-settings

After the file is created, add a new command to reg_replace.sublime.settings. For example:

// Test the RegReplace Sublime Plugin
    "test_reg_replace": {
    "find" : "testxxxxx",
    "replace": "it works!"
   }

image

Step 5: Try it out!

Once you have everything set up, you can use the Command Palette to run your new custom find/replace regular expressions scripts.

image

Note: After installing RegReplace, I received the an error when trying to add my test code to reg_replace.sublime.settings. The error was something like “Cannot save. Can’t create .tmp file in RegReplace folder.” To work around the issue, I opened the security settings for the folder and added write permissions for all users. A bit overkill, but in my case, it’s a secure machine.

No comments: