Back in 2017 I posted my emacs set up for finding files globally. I tend to work with lots of small projects instead of mainly with one at a time. When I use find-file I have to switch folders quite often. If I've already opened the file I can use switch-to-buffer, but then I have to remember whether I have the file open. If I've opened it recently I can use recentf. If I'm opening something in the same project I can use project-find-file. But a lot of times it's easier to use something based on locate, like icicle-locate or consult-locate. But locate is too broad. I want to find one of my files, not one of the files on my disk. I essentially want to treat my home folder as one big project.
In 2014 I was using helm; in 2016 I had switched to ivy; now I use the vertico+marginalia stack. I'm posting the updated code here:
1. Make a list of files
The locate command on Linux and the mdfind -name command on Mac produce a list of filenames on the disk that match some pattern. From that starting point, I filter the list to files that I might want to actually open in Emacs.
- Text files. On Mac, mdfind "kMDItemContentTypeTree = 'public.text' restricts the output to text files. On Linux, I can check the extension.
- Home folder. On Mac, mdfind -onlyin $HOME restricts the list to only files under my home folder. On Linux, I can check the filename prefix.
- Only my files. I want to exclude files from third-party libraries (e.g.
node_modulesin JavaScript).
From an hourly cronjob, I make a global list of filenames that I am likely to work with. This is around 17000 on my system. I sort them so that the newer ones are closer to the top:
(mdfind -onlyin $HOME "kMDItemContentTypeTree = 'public.text' && kMDItemFSContentChangeDate > \$time.today(-1)";\
mdfind -onlyin $HOME "kMDItemContentTypeTree = 'public.text' && kMDItemFSContentChangeDate > \$time.today(-4) && kMDItemFSContentChangeDate <= \$time.today(-1)";\
mdfind -onlyin $HOME "kMDItemContentTypeTree = 'public.text' && kMDItemFSContentChangeDate > \$time.today(-14) && kMDItemFSContentChangeDate <= \$time.today(-4)";\
mdfind -onlyin $HOME "kMDItemContentTypeTree = 'public.text' && kMDItemFSContentChangeDate > \$time.today(-45) && kMDItemFSContentChangeDate <= \$time.today(-14)";\
mdfind -onlyin $HOME "kMDItemContentTypeTree = 'public.text' && kMDItemFSContentChangeDate <= \$time.today(-45)"\
) >~/.global-file-list.txt
From a minutely cronjob, I get everything that I've touched in the last few hours. On my Mac with an SSD, it takes 0.1 seconds to run this:
mdfind -onlyin $HOME "kMDItemContentTypeTree = 'public.text' && kMDItemFSContentChangeDate > \$time.now(-7200)"\
> ~/.recent-file-list.txt
To make the recent-file-list query fast on Linux, fd --changed-within 2hours is probably the best bet. Linux doesn't have a real time filename index like Mac does, so the fd command on my system takes over 10X as long as the mdfind.
2. Static file list
I have these helper functions for reading a text file into a list of strings:
(defun read-file-into-string (filename)
"Read file contents into a string"
(with-temp-buffer
(insert-file-contents filename)
(buffer-substring-no-properties (point-min) (point-max))))
(defun read-file-into-lines (filename)
"Read file, split into lines, return a list"
(split-string (read-file-into-string filename) "\n" t))
Periodically I read .global-file-list.txt into an Emacs list, removing files that are generated from other files. For example if I have apple.md in the list then I'll remove apple.html from the list.
(defvar amitp/global-file-list '() "List of most files I work with.")
(defun amitp/update-global-file-list ()
"Periodically update my global file list."
(condition-case nil
(setq amitp/global-file-list
(amitp/remove-generated-files (mapcar 'abbreviate-file-name (read-file-into-lines "~/.global-file-list.txt"))))
(file-error (message "amitp/update-global-file-list: could not read global file list") nil)))
(amitp/update-global-file-list)
(defvar amitp/update-global-file-list-timer
(run-with-idle-timer 1200 t #'amitp/update-global-file-list))
(defun amitp/remove-generated-files (filenames)
"In a list of filenames, remove the ones that are generated from
another file in the list"
(let ((filename-set (map-into '() 'hash-table)))
(--each filenames (map-put! filename-set it t))
(--each filenames (map-delete filename-set (-amitp/replace-ext ".md" ".html" it)))
(--each filenames (map-delete filename-set (-amitp/replace-ext ".org" ".html" it)))
(map-keys filename-set)))
3. Dynamic file list
The static file list is useful but I want to merge other data sources into it:
- Files from
recentf - Recently modified files (
.recent-file-list.txtfrom above) - Currently open buffers
- Files in the current project
I merge these and delete duplicates:
(defun amitp/all-files-list ()
"Files to list in amitp/all-files"
;; Use destructive delete-dups, much faster than cl-remove-duplicates
(delete-dups
(append
(mapcar 'amitp/abbreviate-file-name
(append
(-take 1 recentf-list)
(amitp/buffer-file-names)
(amitp/remove-generated-files (directory-files default-directory t))
(amitp/remove-generated-files (read-file-into-lines "~/.recent-file-list.txt"))
(amitp/remove-generated-files (amitp/all-files-in-current-project (amitp/project-root-for-directory)))
(amitp/remove-generated-files (-drop 1 recentf-list))))
amitp/global-file-list
nil ;;; need this because append reuses the last element, and delete-dups modifies the list
)))
4. Searching with Vertico
Vertico works with the built-in completing-read:
(defun amitp/all-files ()
"Global filename match, over all files I typically open"
(interactive)
(find-file
(completing-read "All files: "
(amitp/all-files-list)
nil
t
nil
'file-name-history)))
5. Annotating with Marginalia
Marginalia annotates the completion buffer when it knows they're filenames. I can tell it that my command is outputting filenames:
(use-package marginalia :defer t
:config
(add-to-list 'marginalia-command-categories
(cons #'amitp/all-files 'file)))
6. Customization
One thing I love about Emacs is that I can customize it for my needs. I don't think this code will be directly useful to anyone but the ingredients may be useful to remix for your own needs.
Labels: emacs
Post a Comment