Skip to content

Instantly share code, notes, and snippets.

@jdtsmith
Last active January 4, 2026 04:57
Show Gist options
  • Select an option

  • Save jdtsmith/8a6b04a7b0803c1aa50176ee7843c317 to your computer and use it in GitHub Desktop.

Select an option

Save jdtsmith/8a6b04a7b0803c1aa50176ee7843c317 to your computer and use it in GitHub Desktop.
Roll your own emacs consult ripgrep + fd tool
;; An example building your own consult command. Here we take
;; advantage of fd's -X argument to find files with fd (e.g. recent
;; files, etc.) and search them with rg. Consult make this easy.
(defun consult--ripgrep-fd-make-builder (paths)
(let* ((opt-pat (rx (+ space) (group "--") (or (+ space) eos)))
(fd-builder (consult--fd-make-builder paths))
(rg-builder (consult--ripgrep-make-builder nil))
(rg-builder-with-paths (consult--ripgrep-make-builder paths)))
(lambda (input)
(let* ((start (string-match opt-pat input))
(start2 (and start (string-match opt-pat input (match-end 1)))))
(if (null start)
(funcall rg-builder-with-paths input) ; rg-only search
(let ((fd-cmds (funcall fd-builder (concat ". " (substring input start start2))))
(rg-cmds (funcall rg-builder (concat (substring input 0 start)
(and start2 (substring input start2))))))
(cons (append (car fd-cmds)
(unless (or (member "-t" (car fd-cmds))
(member "--type" (car fd-cmds)))
'("--type" "f"))
'("-X") (car rg-cmds))
(cdr rg-cmds))))))))
(defun consult-ripgrep-fd (&optional dir initial)
"Search with `rg' files matched by `fd' in DIR with INITIAL input.
Input is composed like:
rg-patterns (simple `rg'-only search)
rg-patterns -- fd-args (`rg' search over files matching FD-ARGS for `fd')
rg-patterns -- fd-args -- rg-args (`rg' search with RG-ARGS over files
matching FD-ARGS for `fd')"
(interactive "P")
(consult--grep "RG-FD" #'consult--ripgrep-fd-make-builder dir initial))
@jdtsmith
Copy link
Author

jdtsmith commented Jan 4, 2026

It's amazing how good consult is! Someone asked about a consult-like tool with live updating that could perform regexp search, but only within recently changed files.

This consult mashup combines fd with rg, calling the latter with the files produced by the former.

E.g. try entering:

#something -- --changed-within 6hr

to search all files from the current directory with the word something that were changed in the last 6hr. It wasn't too hard to allow ripgrep options to be added too, after a secondary --, e.g.:

#macro flea -- --changed-before 1w --size -3k -- --sort modified

which will search for macro and flea (in any order) among files modified more recently more than 1 week ago and less than 3 kB in size, and then sort them based on their modified time.

Basically all the hard work — process management, asynchronous command handling, match highlighting, file preview, etc. — is handled by consult. Amazing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment