Mac Spotlight doesn't always find matches in .Rmd files

I had the strange experience of Spotlight not finding search words in .Rmd files. When I saved the same files as .R or .txt there was no problem. It did though on occasion find something in an .Rmd file, so clearly they are not ignored completely in searches. Has anyone had this experience or know what to do about it?

I don't use Spotlight for file searches, so I'm spitballing a little here, but it's possible that Spotlight's Documents category doesn't cover .rmd files. Seems odd, if .R files are getting picked up, though :thinking:

What happens if you prepend kind:rmd to your search?

Ddin't help, but good to know about the "kind" option for searching! (It does recognize Rmarkdown as a type of file and is limiting the search to .Rmd files, just not searching them properly.)


Hmmm, weird! Maybe this is a permission issue? Again, seems kind of odd if you can just change the extension and have the search find the term, but... :confused:

Also, have tried doing this search with the Spotlight interface (Cmd+Space) rather than in the Finder (Cmd+Opt+Space)?

EDIT: okay, I think I know what's causing the problem here (though I'm not quite at a solution yet). Spotlight uses things called Spotlight importers to index files—they're plugins for different file types. You can run mdimport to check which Importer is associated with a file:

mdimport -d1 ~/Code/phd-code/raijin/raijin.r 
2017-10-11 07:10:34.290 mdimport[64679:6461357] Imported '/Users/rensa/Code/phd-code/raijin/raijin.r' of type '' with plugIn /System/Library/Spotlight/RichText.mdimporter.

mdimport -d1 ~/Code/thesis/index/index.rmd 
2017-10-11 07:13:27.475 mdimport[64703:6464693] Imported '/Users/rensa/Code/thesis/index/index.rmd' of type 'dyn.ah62d4rv4ge81e5pe' with no plugIn.

To me, this says that .rmd files aren't associated with any Importer on my system, which probably means that they're not indexed properly. I think we need to find a way to associate them with RichText.mdimporter.

EDIT 2: okay, I can't test this ('cause I have indexing turned off), but I believe you need to add the type read off by the mdimport command into the Importer. Note that this is definite "you could screw up your system if this doesn't work and I have no idea whether it will work" territory!

Open up /System/Library/Spotlight/RichText.mdimporter/Contents/Info.plist. You'll probably need to do it as root since it's a system file, so do this by running:

sudo open -t /System/Library/Spotlight/RichText.mdimporter/Contents/Info.plist

You want to add a new line to the highlighted section below:

In my case, the type is dyn.ah62d4rv4ge81e5pe, so I think the line would be:


The type could be different on your system, though (I installed some Quick View plugins a while ago, and that could affect things)—run mdimport on an .rmd file with the -d1 switch to check it (as I did above). I confirmed that a quick test .rmd file has the same type as an existing one.

If you save that, then open up /System/Library/Spotlight/ in the Finder and double-click RichText.mdimporter to re-index, hopefully it should index rmd files now.

I'm sorry that I can't test this! Again, gotta stress: I don't know whether this will work, and it would be very easy to break something here.


Wow, thanks so much, this is so informative. My apologies for not responding sooner. I somehow missed the reply. Given, among other things, my lack of knowledge in the area, I am not going to mess around with things that could screw things up for obvious reasons. The use case that inspired this post was trying the exams package which requires each test problem to be saved in a separate .Rmd file. Since the output file (the complete test) is built from many individual .Rmd files, it's not obvious where the test question is that I need to edit. But there are many safe workarounds, such as saving .txt versions of every file, or knitting each .Rmd file separately to have an .html version for searching. Curious why you have indexing turned off... is it a speed issue? I couldn't survive without being able to search!

That's okay! I think I reached for the nuclear option after I had some indexing problems with exfat USB drives. But to be honest I only really use Spotlight for launching apps—for the most part I either use the Recent Items menu in apps, or I search by file metadata only in Finder (which still works with Spotlight turned off, I think). But maybe I'm doing things the hard way :sweat_smile:

Yeesh, I didn't even realize that Finder and Spotlight are distinct. All of the above difficulty in searching .Rmd files applies to Finder. I also rarely use Spotlight despite my misleading original post. And I should mention that Finder does find search terms in .Rmd files sometimes, which is kind of strange.

Yeah, I'm not entirely sure how they interact—I think the Finder search leverages the deeper indexing from Spotlight if you have it enabled, but I'm spitballing a little :confused:

1 Like

So I think I figured this out! It seems that if the .Rmd file starts with a code chunk, such as here:

it doesn't get searched. When I added a blank line above the code chunk, it did. Odd!