Usually I rely on Projectile’s Helm integration to perform recursive searches with The Silver Searcher (ag) using
helm-projectile-ag (trigged by the completely muscle-memorised
C-c p s s) through all of the org files in my notes hierarchy. Recursive regular expression search results are shown interactively as you type. Wonderful!
However, recently I found that I really missed the ability to see my search results sorted by last modified date, so that the most recently touched files would show up first. For a chronological notes database, this sorting makes the most sense.
A few frustrating hours later, interspersed with bouts of seriously pondering the exact nature of my relationship with Emacs, I managed to attain the desired result using the Emacs packages ivy and counsel.
Although I’m sticking with Helm for the rest, in this case the
ivy API made it much simpler to do what I needed to do. It is understandable that this is not a straight-forward endeavour, as
ag is heavily parallelised and furthermore is handled asynchronously by both
ivy / counsel. Sorting the results slows matters down, but for this specific case the slowdown is a reasonable price to pay.
Read on for more detail.
Video demo of end result
If you follow the instruction below, you too will be able to achieve the following feats of recall:
Search results are returned as I type, as is usually the case with
helm-ag. However, they are now sorted by file modified date, last modified file at the top. This usually means I find what I need faster.
Show me the code
ivy-read function (this is ivy’s single API entrypoint) has two required parameters, one of which is the collection. This is a function returning the list of items that the user wants to narrow down, or the list itself.
Because we need to tell ivy to sort the results by associating a sorting function with a specific collection function, we wrap the supplied
counsel-ag-function collection function.
We also supply a new sorting function that will take two of the ag-generated search results, each of the form
relative_filename:line_number:line_contents, derive the filenames from that, determine the modified timestamps of both the files and then return
nil depending on which file is newer.
We finally (for this first snippet) associate the sorting function with our wrapped collection function.
(defun cpb/ag-collection (string) "Search for pattern STRING using ag. We only have this as a separate function so we can assoc with sort function." ;; this will use counsel--async-command to run asynchronously (counsel-ag-function string "")) ;; modified from https://github.com/abo-abo/swiper/wiki/Sort-files-by-mtime ;; * directory to counsel--git-grep-dir: that's where AG is working ;; * removed directory logic: ag returns files ;; * parsed out filename from ag results (defun eh-ivy-sort-file-by-mtime (x y) "Determine if AG sort result X is newer than Y." (let* ((x (concat counsel--git-grep-dir (car (split-string x ":")))) (y (concat counsel--git-grep-dir (car (split-string y ":")))) (x-mtime (nth 5 (file-attributes x))) (y-mtime (nth 5 (file-attributes y)))) (time-less-p y-mtime x-mtime))) (add-to-list 'ivy-sort-functions-alist '(cpb/ag-collection . eh-ivy-sort-file-by-mtime))
In the second and final snippet, we create a new version of the
counsel-ag function where we specify our own collection function, and we supply a truthful
(defun cpb/counsel-ag (&optional initial-input initial-directory extra-ag-args ag-prompt) "Grep for a string in the current directory using ag. INITIAL-INPUT can be given as the initial minibuffer input. INITIAL-DIRECTORY, if non-nil, is used as the root directory for search. EXTRA-AG-ARGS string, if non-nil, is appended to `counsel-ag-base-command'. AG-PROMPT, if non-nil, is passed as `ivy-read' prompt argument. Modified by cpbotha: Sort results last modified file first." (interactive (list nil (when current-prefix-arg (read-directory-name (concat (car (split-string counsel-ag-base-command)) " in directory: "))))) (ivy-set-prompt 'counsel-ag counsel-prompt-function) (setq counsel--git-grep-dir (or initial-directory default-directory)) (ivy-read (or ag-prompt (car (split-string counsel-ag-base-command))) ;; specify collection function so we can assoc with sort function #'cpb/ag-collection :initial-input initial-input :dynamic-collection t ;; yes, we want to sort the results :sort t :keymap counsel-ag-map :history 'counsel-git-grep-history :action #'counsel-git-grep-action :unwind (lambda () (counsel-delete-process) (swiper--cleanup)) :caller 'cpb/counsel-ag))
You can invoke
cpb/counsel-ag interactively to search from the current directory, or you can point it at a directory of your choosing by passing in the
In my Emacs initialisation, I have bound another wrapper function that invokes
cpb/counsel-ag on my notes directory, so I can instantly search through my notes in reverse chronological order.
I believe that my relationship with Emacs is safe for now.