Building small productivity boosters is one of my favorite things to do when I code. These projects usually involve writing a little bit of code that in some way makes my life easier. It can be a big undertaking, like a Slack integration, or a small one, like a command line utility. My latest creation is the latter: a command line alias that changes my Sublime Text theme.
Why would I need to change my theme that often, you ask? Syntax highlighting. See, working on client projects requires that I work in a few different languages and frameworks, including Ruby, AngularJS and React. Switching between them can be challenging and syntax highlighting is my visual guide for organization and errors.
Fortunately, there’s a Sublime theme that works well with Babel: the delightfully named Broceanic. When I work on a React project I prefer this theme over Predawn. It’s not as understated, but as a new React developer I’ve found this theme to be extremely helpful.
To switch Sublime themes you normally have to go in through the user preferences file and make the changes by hand. It only takes a few clicks and some cutting and pasting, but doing that repeatedly got old quick for me. Luckily, there’s a handy command line utility that will search a file for a given string and replace it with another, sed:
sed -i s/‘text you want to replace’, /‘text you want to replace it with’,/g’ /path/to/file-that-includes-text.json
Running the sed command shown above will navigate through the given file path to the specified file, find the desired text and replace it with the new string. If you want the changes to be written to the same file, instead of a new one, give sed the “in-place” option: -i. Also note that in some cases on Mac machines you may need to include an empty string ‘ ’ in between the -i option and your text.
To change my Sublime theme I targeted two lines in my Preferences.sublime-settings file:
Using sed I can change color_scheme to “Packages/Oceanic Next Color Scheme/Oceanic Next.tmTheme” and theme to “Broceanic.sublime-theme”. This will change my theme from Predawn to Broceanic. Changing the theme back is done the same way but with the correct Predawn files.
All I need to do to make this trick easy to use is to alias the commands as Brocean and Predawn. Check it out:
Pretty cool, right? It’s a simple hack that was easy to build, saves me a little time, and gives me a lot of joy each time I get to type Brocean into the command line.