Mmmmmmm, Text Pastries

Submitted by Josh on
Do you use Sublime Text? You should, it's great. I used other tools for web development for the last ten years or so, but early in 2016 I made the change to SublimeText and I don't think I can ever go back. I was sold the first time I was able to download a community-sourced plugin to provide accurate syntax highlighting for a LESS file, and sold even harder the first time I wrote a macro to generate a bank of similar table rows with a key combination.
 
The coolest thing I've seen, though, might be Text Pastry. It's an open-source plugin for Sublime that extends the already-cool Multiple Selections feature to make wide-spread changes with more granularity than simple find and replace, or, even more importantly, without making find and replace a simple one-to-one type of conversion. Check out the feature list, and note cool things like being able to preset lists of dictionary terms (the examples given are days of the week and Greek letters), and replacement with patterns such as single-character repetition with x iterations or complex pattern repetition over x iterations. And I assume the name is a pun on "pasting," which is pretty awesome too.
 
I came across this when running into a testing problem for a project I was doing at work back in November. I was writing a script to investigate some user-generated data in a compressed blob in our database; the script would take the user's data, decompress it, search it for a particular marker, and then take further action based upon that marker's status. To see if it would be performant, I needed a lot of data, some with the marker and some without, and I needed that data to live on my dev environment just in case something went horribly wrong - possibly altering things that a user can see, even if innocuous, is not something you typically want to do live.

So, I grabbed a dataset of a thousand or so records from a more robust environment, dumped them out so I could load them into Sublime, cloned them over and over until I had about twenty thousand, and then conjured up a multiple selection using regex to find users' machine IDs. I was then able to use Text Pastry to intelligently replace the IDs with new values that incremented and were far enough out of scope that they would never be mistaken for real users if seen again, using the

\i(int, int)

pattern in Text Pastry. Total time? Roughly 20 minutes from pulling the data to loading the fresh, larger dataset into the dev database. I was then able to run my script and verify that it did what I wanted against twenty thousand records, saving more time and worry as the script made its way to prod.

In low-risk, low-consequence jobs, this kind of thing could be used to iterate through all kinds of data almost on the fly, saving lots of time. For higher-risk jobs like the one described above, it still freed me up to move on to other things faster and with less worry about server load and script speed than I would have been able to get with many days' work otherwise. And it's free and open source, to boot.