Find & Replace in Sublime


I need to find and replace strings in several .xhtml files. Usually I do this manually in Sublime text editor, but I'm trying to find a way to automate the process. Is there a way to create a macro that will prompt me for the original string and the replacement string and then find & replace each instance? Would I open each file in Sublime, or is there another way to do it?


Various approaches here (using grep for example):

How to replace a string in multiple files in command line - Stack Overflow

Could be adapted for a macOS command line in a Keyboard Maestro Execute a Shell Script action.

I don’t use Sublime Text, but I think it has a way of treating a collection of files as a “project,” and once you’ve done that you can do the search-and-replace across the entire project instead of one file at a time.

Yes that's exactly what I do manually (great feature). I'm just trying to figure out if there's a way to automate that process with KM. I'm struggling with how to enter the original string and replacement string within Sublime using KM.

I confess I'm having trouble understanding how entering two strings in Keyboard Maestro could be much more efficient than entering those same two strings in Sublime. Are there other aspects (clicking radio buttons or checkboxes) of doing the search/replace in Sublime that are slowing you down? If so, you probably can automate those aspects of the process by having Keyboard Maestro tab through the controls and activate the ones you need.

This bit can be tricky -- when you are using a KM prompt to set a variable to make a regex to paste into an app you get some funky text processing flows. See this post from @peternlewis for a better explanation than I could ever manage!

Given that, and the fact you'll be filling in a dialog whatever you do, I'd echo @drdrang and start by automating everything but search/replace string insertion. You could then deep-dive into @peternlewis's token processing explanation if you found you wanted to take things further.

1 Like

Using the Insert Text by Pasting action?

Something like this workflow?

  • Activate the search/replace dialog via hotkey or menu-selection.
  • Insert text.
  • Tab.
  • Insert text
  • Return

Using the Pause Until action with the found image condition as needed – using the Move or Click Mouse action with a found image condition as well.

I don't have Sublime, so I'm having to conjecture – but I've done this sort of thing with other apps.

Are you trying to do this on demand or build a stack of pre-made replacements?

You can use the Prompt for User Input action if you're needing interactive access.