Amanda Olsen

Front End Developer User Experience Enthusiast


Thoughts on the industry

Write a Macro and an Advanced Find/Replace for Sublime Text 2

I was recently given the task of converting many PDFs to HTML. This is not fun. However, it’s fun if you get to play with the Sublime Text 2 settings/packages/amazing stupedous power. Here’s what I wanted to do:

  1. In Adobe Reader, save PDF as HTML. Against that, do all of the following.
  2. run HTML Tidy
  3. delete doctype tag
  4. delete opening/closing HTML tags, and closing Body tag
  5. unindent two tabs
  6. delete opening Body tag
  7. delete head tag (and all child tags)
  8. delete style blocks
  9. delete all br tags
  10. delete all •
  11. delete all classes
  12. delete all span tags
  13. remove all width and height attributes from image tags
  14. replace “ with “
  15. replace ” with “
  16. replace ’ with ‘
  17. replace copyright symbol ©
  18. replace trademark sybmol ™
  19. replace registration symbol ®
  20. reconnect words that have been randomly broken into multiple “words”
  21. convert all totally uppercase strings to “Title Case”
  22. wrap each section in the appropriate tag
  23. style as needed (should hardly need anything given the previous step and the fact that styles have already been written)

Using Sublime Text, steps 2 through 19 could be automated. Here’s how.

  1. Install “HtmlTidy” package
    1. Oen Command Palette, type “install package”, type “HtmlTidy”.
    2. This assumes you’ve already installed the package “Package Control”. If no, do so.
  2. Install “RegReplace” package
    1. Open Command Palette, type “install package”, type “RegReplace”.
    2. It actually appears this is the only way to automate a large set of find/replace commands in Sublime Text. Rather surprising, but true. Anyway, this package works great.
  3. Read up on Macros. It’s really, really easy. Mostly you just need to know ctrl+q. This command both starts and stops recording of a macro.
  4. Record a macro for steps 2 through 5. Ctrl+q to both start and stop it. After that Tools -> Save Macro. Name it for future use.
  5. Start using RegReplace to do steps 6 through 19.
    1. You’ll want to copy over two files, and then customize those files to your heart’s content.
  6. (There’s actually more that could be said here, more detail, but I’ve run out of time, and my memory is bad. Either way! This post is mostly complete and still has some value.)

My sources:


Share your thoughts

Your email address will not be published.

two + 1 =