reddit / emacs
What's the easiest way to `find-file` ANY file below CWD? (Like the Idea/Intellij apps)
campbellm · 2022-03-27 15:05
I'm looking for a (canonical?) way for C-x C-f to be able to complete, ideally in a "fuzzy" way, files 'below' emacs' notion of current-working-directory.
I tried find-file-in-project, but either I misunderstand it, or it misunderstands me; I *DON'T* want want an easier way to trapse down a directory tree, I want to avoid having to know the directory tree altogether if I know the file name. For example:
C-x C-f a_i_s # or as.r, or ainvse
and have it suggest for example, `app/whatever/full/path/to/autopayment_invoice_service.rb`
I found a similar question here: https://www.emacswiki.org/emacs/TextMate, but I'm not sure I understood the answers, or they just didn't quite fit what I'm trying to do.
Something with tags, maybe?
Pointers welcome
ieure · 2022-03-27 15:56
Install projectile and use `projectile-find-file`.
00-11 · 2022-03-27 15:57
Vanilla Emacs also has command `find-name-dired` (and similar commands in library `find-dired.el`), which find files by name in or below a given directory. It accepts glob patterns.
----
If you use [Icicles](https://www.emacswiki.org/emacs/Icicles) then you can use commands [`icicle-locate` and `icicle-locate-file`](https://www.emacswiki.org/emacs/Icicles_-_File-Name_Input#FindFilesAnywhere) to find a file anywhere. As always with Icicles, you can use regexp (aka [apropos](https://www.emacswiki.org/emacs/Icicles_-_Apropos_Completions)) matching and various kinds of [fuzzy](https://www.emacswiki.org/emacs/Icicles_-_Completion_Methods_and_Styles) matching.
----
Vanilla Emacs also has a function `locate-file`, but it's not a command (thanks, /u/oantolin, for the reminder).
> Search for `FILENAME` through `PATH`.
>
> If found, return the absolute file name of `FILENAME`; otherwise
return `nil`.
>
> `PATH` should be a list of directories to look in, like the lists in
`exec-path` or `load-path`.
>
> If `SUFFIXES` is non-`nil`, it should be a list of suffixes to append to
file name when searching. If `SUFFIXES` is `nil`, it is equivalent to (`""`).
>
> Use (`"/"`) to disable `PATH` search, but still try the suffixes in `SUFFIXES`.
>
> If non-`nil`, `PREDICATE` is used instead of `file-readable-p`.
>
> This function will normally skip directories, so if you want it to find
directories, make sure the `PREDICATE` function returns `dir-ok` for them.
>
> `PREDICATE` can also be an integer to pass to the `access` system call,
in which case file name handlers are ignored. This usage is deprecated.
For compatibility, `PREDICATE` can also be one of the symbols
`executable`, `readable`, `writable`, or `exists`, or a list of
one or more of those symbols.
[deleted] · 2022-03-27 15:58
Or project-file-file on newer versions of Emacs/after having installed [project](http://elpa.gnu.org/packages/project.html).
wskydev · 2022-03-27 17:50
You can use 'project-find-file' `C-x p f`. I'd recommend taking a look at all project related functions. I use `C-x p g` 'project-find-regex' quite a bit to search through project files.
Reference:
[https://www.gnu.org/software/emacs/manual/html\_node/emacs/Projects.html](https://www.gnu.org/software/emacs/manual/html_node/emacs/Projects.html)
oantolin · 2022-03-27 18:44
Weird: my Emacs doesn't have `locate-file`. There is a `locate` command which depends on the external program `locate`.
oantolin · 2022-03-27 19:02
For the most part the type of matching in Emacs can be configured separately from the type of thing you are matching. To find files at arbitrary depth under a directory you can use among other commands:
- `project-find-file` as others have mentioned, but this requires your directory to be a project. What is considered a project is extensible, but by default version controlled repositories are considered projects. This command is from the [project](https://elpa.gnu.org/packages/project.html) package which comes with recent enough Emacs, but you can always install from GNU ELPA.
- `consult-find` from the excellent [consult](https://github.com/minad/consult) package; this has the advantage of working for directories that are not version controlled too.
To control the type of matching for those commands, and in general for minibuffer completion, you can set the value of the variable `completion-styles`. There is a built-in "fuzzy" completion style called `flex`, which you can quickly check out by activating `fido-mode`. Of the built-in styles I also find `partial-completion` good for files (`/u/s/d/w` would match `/usr/share/dict/words`, for example). I shouldn't say this myself, but by far my favorite completion style is the [orderless](https://github.com/oantolin/orderless) style I wrote. This will let you type space-separated patterns that can match in any order, and the patterns match in configurable ways including flex (your "fuzzy") or something similar to partial completion.
The default completion UI for Emacs does not show the matching files until you display the completions buffer by either pressing TAB once or twice or by pressing ?. You may want to choose a completion UI which alwyas shows the matching files and updates the list in real-time. The built-in `fido-mode` which uses the `flex` style, and which I mentioned above is one such UI. Also built-in is `icomplete-mode`. But I highly recommend the external [vertico](https://github.com/minad/vertico) package for this purpose.
00-11 · 2022-03-27 19:31
1. Function `locate-file` is in GNU Emacs since at least release 22 (in `files.el`). (But as you pointed out below, I was mistakenly in thinking it's a command.)
2. Yes. Command `locate` depends on the external program `locate`. That's why there is also `locate-file` (and similarly why there are two Icicles commands).
oantolin · 2022-03-27 20:51
Oh, I see `locate-file` now, but you are wrong about it being a command, it is a non-interactive function.
00-11 · 2022-03-27 22:13
Oops, sorry, you're right about that!
I've edited my answer to point that out. Sorry for misleading.
(I only use `icicle-locate-file`, which is a command. I was thinking that the vanilla function was also a command.)
karthink · 2022-03-28 14:54
Also consult-dir (on MELPA) includes an easy way to invoke consult-find from the find-file prompt, that will search for whatever you've typed in under the current directory.
oantolin · 2022-03-28 15:22
I even have `consult-dir-jump-file` bound to `M-j` in `minibuffer-local-filename-completion-map` and keep forgetting to use it! (I do use `consult-dir` multiple times a day, though.)
redguardtoo · 2022-03-29 13:11
https://github.com/redguardtoo/find-file-in-project#find-file-in-current-directory