Assume I have a bunch of directories and can guarantee that no two directories have a common filename (i.e. if foo/bar exists, then bar doesn't exist in any of the other directories). How can I setup emacs such that, given a hard-coded list in .emacs of the directories to search, I can tell it to open bar and it figures out that it's in foo rather than me needing to tell it foo/bar every time? It'd be even better if it handled name collisions somehow (e.g. a list of possible matches), but that's not mandatory as I'm certain in this case the directories have no filenames in common


As I write this, I discover ifind-mode, which looks spot on.

(setq workspace-dir "/path/to/dir1 /path/to/dir2")
(require 'ifind-mode)
;; maybe change `ifind-command' as well

Then type M-x ifind-mode to find a file in the specified directories (and subdirectories by default), with nifty completion.

What I usually do is e **/bar from zsh (where e is an alias for emacsclient). But that's disruptive if you're in Emacs already. Also, if there's a specific file I edit often, I leave it open (and I save my session).

Out of the box, you can run M-x find-dired and specify arguments to find. But that's clumsy, at least for the case when you have a single match. ifind makes this less clumsy.

ido has a bunch of features that may be useful, especially Use ido to find any file from the project.

  • That looks very handy – Faheem Mitha May 17 '11 at 21:27

I just discovered filecache, which is perfect for my purposes. There are several functions of the form file-cache-add-directory-X to add files to the cache, and then in the middle of typing a filename in find-file you can hit C-Tab to autocomplete the path using the cache (hitting it multiple times cycles through matches)

The EmacsWiki post includes functions to save/restore the cache. I wrote a cron job to generate the cache file automatically every night; the file looks like this:

(("filename" "/path/to/that/file/") ("other filename" "/different/path/"))

Then I automatically load from the cache when emacs starts using the function mentioned on EmacsWiki:

(require 'filecache)
(defun file-cache-read-cache-from-file (file)
  "Clear `file-cache-alist' and read cache from FILE.
The file cache can be saved to a file using
  (interactive "fFile: ")
  (let ((buf (find-file-noselect file)))
    (setq file-cache-alist (read buf))
    (kill-buffer buf)))

(file-cache-read-cache-from-file "~/.file-cache")

There're couple of alternatives, e.g: recentf-mode and ido-find-file from ido package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.