Searching code on Mac

Most of the heavy lifting for data processing and GIS is done on linux computers, but a lot of development and nearly all of my writing and thinking happens on a Mac laptop. There are a lot of tools that I rely heavily on for organizing ideas and information.

I also rely heavily on search for finding things, using basic Spotlight and the excellent HoudahSpot. These tools are great for searching file names of any sort, but Spotlight wwill only search and show previews for files with extensions it recognizes, regardless of what the file type actually is. I use *.Rmd for R markdown files and *.rbat for certain kinds of R scripts, and has been frustrating to be unable to search them.

After reading a bunch of articles and some trial and error, I’ve figured out how to teach Spotlight to recognize text files with custom extensions. Most of the search results I found gave information that does not work with newer versions of Mac OS because of changes to the security settings. The following works on Catalina.

First figure out what your file types are being classified as. You’ll need a terminal for this and subsequent steps.

mdimport -d1 -t filename

will tell you what Spotlight thinks your file is. I tried three different file types, all of which are plain text.

  • Imported ‘myfile.R’ of type ‘’ with plugIn /System/Library/Spotlight/RichText.mdimporter.
  • Imported ‘myfile.Rmd’ of type ‘dyn.ah62d4rv4ge81e5pe’ with no plugIn.
  • Imported ‘myfile.rbat’ of type ‘dyn.ah62d4rv4ge81e2xbsu’ with no plugIn.

The *.R file was recognized as text, and Spotlight knew to use the RichText.mdimporter to process it, but the other two file types were recognized as “dyn.randomstring” and Spotlight couldn’t figure out what to do with them.

It’s straightforward to modify RichText.mdimporter to create your own custom Spotlight importer. I renamed mine to RCode.mdimporter but you may not need to rename it since you are putting the new importer in your personal library, and not touching the system file.

First, make a directory to hold Spotlight files in your local library, and then copy the system Spotlight importer to it.

mkdir ~/Library/Spotlight
cp -r /System/Library/Spotlight/RichText.mdimporter ~/Library/Spotlight/RCode.mdimporter

Now open the file ~/Library/Spotlight/RCode.mdimporter/Contents/Info.plist with your favorite text editor. Refer back to the results of the first command, because you’ll need the “dyn.randomstring” information. Look for the section on LSItemContentTypes and add the content type for your text files to it, in the same format as the other types listed. For my *.Rmd and *.rbat files, the lines to add are:


but use your own results from mdimport.

Save and close that file. The only thing left is to tell Spotlight to reindex your files using the modified definitions.

mdimport -r ~/Library/Spotlight/RCode.mdimporter

It took a few minutes to complete, but now when I search, I get full-text preview in HoudahSpot for my R code files.