Skip to Content

Fulltext Search in org-mode

Quite often I’m trying to find something that I jotted down in one of my many org-mode files somewhere. This usually amounts to something like

M-x rgreg RET foo RET *.org RET ~/ RET

Then I wait until grep has finished rummaging trough my home directory. Wouldn’t it be nice if there was something like fulltext search for org-mode files, i.e. if there was a tool that would index all my files and provide for a fast search capability?

Well, enter Code Search

sudo apt install codesearch

Then build an index of all your org-mode files

find . -type f \( -name \*.org \) -print | xargs cindex

When searching I’m old school. I want the tool to behave just like rgrep : give me a buffer where I can navigate through the hits. That’s why I invoke rgrep in a round-about way:

C-u C-u M-x rgrep RET C-a csearch -n foo RET

This will basically re-use all of the rgrep functionality but use csearch as the actual program to execute.

I’m sure this could be improved with a tiny bit of elisp or you could explore a dedicated package named emacs-codesearch. I haven’t tried it but from reading the source it doesn’t seem to provide that standard grep mode buffer.

Implementation

In the end I came up with the following snippet in my Emacs config. This gives me a new function cgrep that simply invokes csearch with the right options and dumps the result in a compilation buffer, just like rgrep does.

(defun cgrep (regexp)
  (interactive (list (grep-read-regexp)))
  (compilation-start (format "csearch -n %s" regexp) 'grep-mode))

Flip side

By using codesearch you get blazingly fast search, that’s for sure. There is a downside however: The list of files that you will be searching is fixed at the time you create the index. So if you want to search only a part of your org-mode files you will have to re-create the index before searching, which of course makes the search much slower than just using plain old rgrep.