Using consult-ripgrep with org-roam for searching notes

I find deft pretty slow, and even notdeft isn’t as fast as I would like (and is harder to set up). ripgrep is very fast though, and the consult package includes a very nice consult-ripgrep function.

Assuming you have consult installed, here’s a useful function:

(defun bms/org-roam-rg-search ()
  "Search org-roam directory using consult-ripgrep. With live-preview."
  (let ((consult-ripgrep-command "rg --null --ignore-case --type org --line-buffered --color=always --max-columns=500 --no-heading --line-number . -e ARG OPTS"))
    (consult-ripgrep org-roam-directory)))
(global-set-key (kbd "C-c rr") 'bms/org-roam-rg-search)

brief demo:

(ripgrep does have a --files-with-matches option, which in theory would produce output more similar to what deft/notdeft returns, but I haven’t figured out how to use it via consult-ripgrep yet.)


Very nice! Thank you. Will try it

Thanks for posting this. I installed the consult package and ran your function, but it didn’t return any matches. I also tried just running consult-ripgrep but that didn’t return anything either. Do I need to do anything to initialize consult-ripgrep?

Do you have ripgrep installed on your system? (it’s external to emacs)

I do. I can get consult-ripgrep to work on vanilla Emacs, but not on my Doom config (or a fresh Doom config). I’ve logged an issue.

Any alternatives? I love the idea of ripgrepping my Org/roam folder and then having previews of the files as I scroll through the results.

@emacsomancer Thanks again for this post. It will be useful for my set up.

I wanted to make ripgrep search to case-insensitive globally (otherwise I won’t find what I should find), so I did a very small customization to add --ignore-case like this.

Just wanted to share it in case someone else wants to the same thing.

;; --ignore-case is added, not part of the default
   "rg --ignore-case --null --line-buffered --color=always --max-columns=500   --no-heading --line-number . -e ARG OPTS")

You might also look at the rg.el package, though you’d have to think about how get previews on scroll, I think.

For using consult-ripgrep, my guess is that Doom probably uses some other completion framework by default (Helm, Ivy) that’s interfering.

You may want to alter the function too: I have the --smart-case option enabled there (which ignores case as long as you don’t type any CAPS), you might want to replace that with --ignore-case, if you prefer that because my function over-writes the global consult-ripgrep options for its search. (I’m not sure which option I prefer - I usually just search in all lowercase so it probably doesn’t matter).

Oh i see. Thank you. I was happy with the ignore-case option, but i’ll look up the smart-case one to see which i prefer :slight_smile:

revised code to be a bit cleaner, and changed to ignore-case rather than smart-case as the former is probably more transparent.

[edited original post, but also reproduced here:

(defun bms/org-roam-rg-search ()
  "Search org-roam directory using consult-ripgrep. With live-preview."
  (let ((consult-ripgrep-command "rg --null --ignore-case --type org --line-buffered --color=always --max-columns=500 --no-heading --line-number . -e ARG OPTS"))
    (consult-ripgrep org-roam-directory)))
(global-set-key (kbd "C-c rr") 'bms/org-roam-rg-search)


And here’s my initial attempt at a more deft-like version, which just returns one file match per term no matter how many hits are in the file. It doesn’t have the nice live-preview of the above function though:

(defun bms/consult-ripgrep-files-with-matches (&optional dir initial)
  "Use consult-find style to return matches with \"rg --file-with-matches \". No live preview."
  (interactive "P")
  (let ((consult-find-command "rg --ignore-case --type org --files-with-matches . -e ARG OPTS"))
    (consult-find dir initial)))

(defun bms/org-roam-rg-file-search ()
  "Search org-roam directory using consult-find with \"rg --file-with-matches \". No live preview."
  (bms/consult-ripgrep-files-with-matches org-roam-directory))
(global-set-key (kbd "C-c rf") 'bms/org-roam-rg-file-search)
1 Like

FYI, there’s work (that jethro started actually) going on to add selectrum and consult as an alternative to ivy and helm.

1 Like