Last active
January 4, 2026 04:57
-
-
Save jdtsmith/8a6b04a7b0803c1aa50176ee7843c317 to your computer and use it in GitHub Desktop.
Roll your own emacs consult ripgrep + fd tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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)) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
fdwithrg, calling the latter with the files produced by the former.E.g. try entering:
to search all files from the current directory with the word
somethingthat were changed in the last 6hr. It wasn't too hard to allowripgrepoptions to be added too, after a secondary--, e.g.:which will search for
macroandflea(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!