Quick Tip: This command line trick is tops

Quick tips are random posts regarding something I discovered on my way to something bigger. They usually get longer than “quick” would imply, for which I refuse to apologize.

I just (accidentally) discovered the command tops while working in Terminal. I’m not seeing many of mentions of it (any) on the web, but it could be too early in the morning for me to form a coherent query. Regardless, I thought I’d give it a mention. The man page is quite complete, and has a succinct explanation of the command: tops - perform in-place substitutions on code.

It’s not just useful for code, though, it makes a great tool for batch modifications to any text file(s). With an easy-to-grok syntax, it does in a few words what I usually do in three or more piped oneliners (grep, awk, sed, etc.). There’s a replacemethod option specifically for replacing Objective-C-style method names, which leads me to believe that this is an OS X-only tool, possibly only part of the Developer Tools. If anyone more familiar with this command has insight, I’d be curious.

Tops can read a script file with rules for search and replace, or take commands as arguments to process changes in a text file instantly. Simple example:

tops replace "Word" with "Markdown" writing.*

All instances of “Word” in all files with the name “writing” were just replaced with “Markdown.” Try it, it’s cool. The options go far beyond my simplistic example, but the more complex options are primarily specific to Objective-C. If this piques your curiosity anyway, spend some time with man tops.

Brett Terpstra

Brett is a writer and developer living in Minnesota, USA. You can follow him as ttscoff on Twitter, GitHub, and Mastodon. Keep up with this blog by subscribing in your favorite news reader.

This content is supported by readers like you.