Sublime Forum

Integrated external project search and replace

#1

Overview

Some of you may use RegReplace. There’s been multiple requests to be able to use the regular expression chains in RegReplace against a project instead of just a file. I’ve held the stance that I wasn’t going to add the ability to scan projects from within Sublime, and that hasn’t changed. Never say never, but I have no immediate plans for it at least.

Sublime’s project search is fine, but I’ve always been annoyed that I specify my search and file filter on the same line. I also don’t like how it opens files to replace content etc. So I like to use a dedicated external tool. I like grep and sed alright, but for whatever reason, I’ve always enjoyed a decent graphical search tool. There are many on Windows, but less decent ones on macOS and Linux, but maybe that’s changed as I haven’t looked recently.

Anyways, I thought I’d share what I do in case there are people who desire a similar setup, and people can adapt it how they like (you can even use a similar approach for other things, maybe opening images in an external editor from the sidebar etc.).

What I Do

I use a plugin I wrote (AnyOpen) to send folders and files from the sidebar context menu or the view’s context menu to external applications. I’ve had it for a while in some form, but only recently put it in it’s own repo and generalized its functionality. It’s not on Package Control, and maybe there is already something similar on Package Control (I haven’t checked). I guess if people wanted it on Package Control, I could accommodate.

So basically I set up context entry rules in AnyOpen and I send folder paths or file paths to my preferred application. If the tool supports initializing search terms, you could tweak all this to send the current selection to your external tool, but I don’t really support that at the moment, I keep it pretty simple.

I then setup the external tool to open results in my favorite editor (Sublime of course :slight_smile:).

Rummage

As mentioned before, Windows has a number of search and replace tools, but I never found one on Linux and mac OS that suited me. And on top of that, I like a consistent work flow across my different systems. So I wrote my own that is cross platform. It’s similar to RegReplace in the idea you can optionally save away your search. You can also create chains and apply a search chain in a “chain mode”. You can also assign advanced replace plugins to run a Python script to do the replace instead of a pattern. You don’t get RegReplace’s Sublime scope context as it isn’t Sublime specific, but you can use re or the regex module just like RegReplace, and even use the optional backrefs module to add a couple new optional back references to either re or regex.

Rummage is written in Python and runs on Python 2.7 and Python 3.4+ and installable via pip (Linux requires some dependencies to be installed before running pip so it can properly install wxPython – see documentation).

Rummage performs pretty well when you are specifying (forcing) an encoding, but it can guess encoding (using the chardet library), but it is limited by all of chardet’s limitations (like slowness for instance since it is pure Python). On small projects, encoding guessing is fine, but I am rarely dealing with projects with multiple encodings, and if I need to do multiple encodings, I zero in on the specific folder to limit how many files I’m guessing encoding.

Conclusion

Maybe there is something useful in here for someone, or maybe not. Everyone has different workflows or requirements, but at the very least, maybe this sparked some ideas, or maybe something listed above can be applied to a problem someone is trying to solve.

If you try and do a similar setup above (with these tools or something similar), remember to read the documentation of the specific component’s setup.

6 Likes

Search & Replace in files question
#2

Just for people attempting to use Rummage on macOS. WxPython can be funny when not run in a “Framework” build of Python or in a virtual environment. So if you installed Python via brew, Anaconda, or trying to run this from a virtual environment etc., you may run into this issue

This program needs access to the screen
Please run with a Framework build of python, and only when you are
logged in the main display of your Mac.

This doesn’t mean Rummage is broken, but this is a common error for WxPython if not run in a framework build, or better put, not able to detect where the framework is.

There are workarounds to get this working: see https://wiki.wxpython.org/wxPythonVirtualenvOnMac. Also check out the issues for your specific environment brew, anaconda, etc., as they may have a better specific solution for your particular environment.

I install my Python builds on macOS with the installers from Python.org, and I don’t run them through a virtual environment, so if anyone works through this issue in a specific environment like brew and anaconda, please do share, and I can add some pointers in the docs.

0 Likes

#3

Seems the above error isn’t really a big deal. This used be more of a nuisance in wxPython 3, but seems to be less so in wxPython 4. The wiki link I posted seems to be out of date and probably only applies to wxPython 3. I’ve posted my findings working through virtualenvs, brew, and Anaconda in the documentation.

In my findings installing in brew and Anaconda didn’t really require anything extra, but you do have to run the tool with pythonw not python.

Virtualenv fix was trivial.

1 Like