LowCom · 2022-06-15 05:42
So every time I find a file, I do C-x C-f and then /**/fileName.
What it does is it recursively searches through the current directory to find the file I want. This allows me to not specify the exact subdirectory the file is in.
I want to write a small wrapper over find-file function which automatically does this and then map a key to it.
So for eg, when I type C-x C-r (my key map for this new function), it should prompt me with
Find-file: current-directory/**/
I would also be content with any other way to recursively find a file in the sub directories of a folder. It is insane, this is so hard to do in emacs. It's a breeze in vim. I don't want to use projectile and all, my folders are just plain text notes, they are not code files or projects most of the time.
whudwl · 2022-06-15 06:23
https://github.com/CsBigDataHub/counsel-fd would do what you want
0lMon · 2022-06-15 06:37
Hej Hej,
(defun fancy-shmancy-find-file ()
(interactive)
(let ((default-directory (concat default-directory "**/")))
(call-interactively #'find-file)))
> It is insane, this is so hard to do in emacs.
Only insane thing is, I wrote that on my smartphone only having emacs available in termux to checkout the documentation.
Now you just need to bind it to your preferred keybinding.
Cheers!
ainstr · 2022-06-15 06:52
I'm not sure if its my setup but using /**/FILE is not recursive for me, its only 1-deep. There's `globstar` in bash, but I'm not sure how emacs regex works in this case.
I dug through `project.el`, which is a built-in, and this seem to work:
(find-file
(project--read-file-cpd-relative
"Recursive Find: "
(project--files-in-directory default-directory nil)))
Its derived from `project-find-file-in`, I just nullified any actual project-based information. This is probably overkill.
LowCom · 2022-06-15 07:39
It worked, thank you. (Thought a "/" is missing before **)
I need to learn more elisp. The manual feels so dry. A more interesting resource would be good.
LowCom · 2022-06-15 07:42
> I'm not sure if its my setup but using /**/FILE is not recursive for me, its only 1-deep
Oh shit. This is super annoying, I too just noticed it's only one level deep. Why is this so bad?
arthurno1 · 2022-06-15 20:20
> I would also be content with any other way to recursively find a file in the sub directories of a folder.
I guess you haven't been reading docs: [directory-files-recursively](https://www.gnu.org/software/emacs/manual/html_node/elisp/Contents-of-Directories.html) is what you ask for.
> It is insane, this is so hard to do in emacs.
Is it? You can use directory-files-recursively directly via M-:, if you are "content with any other way" as you say, or you may wrap it in some interactive function for somewhat nicer user experience. Here is a version that resembles find-file which you can use with M-x:
(defun find-file-recursively ()
(interactive)
(let* ((files (directory-files-recursively "./" directory-files-no-dot-files-regexp t))
(file (completing-read "Open file: " files ))
(value (find-file file)))
(if (listp value)
(mapcar 'pop-to-buffer-same-window (nreverse value))
(pop-to-buffer-same-window value))))
How hard was that?
But honestly, what you ask for is generally, probably a dumb idea. I would not execute that on my home folder or on some other big directory with lots of subdirectories or files. I guess that is a reason why such function is not already included in Emacs. However, you can use the above in a directory where you know there are not thousands of files in arbitrary deep directory levels.
LowCom · 2022-06-16 02:30
Oh, this works. Thank you. I will add a keyboard shortcut to it so I can use it often.
Why would you not recommend this though? Because it will be slow if the folder is large? But then I could just navigate to a subfolder and then run this. Is there any other consideration? How do *you* personally find files?
Also can you please comment on the code so I can understand what it's doing?
arthurno1 · 2022-06-16 04:49
> How do you personally find files?
helm-find-files
redguardtoo · 2022-06-17 00:56
https://github.com/redguardtoo/find-file-in-project#find-file-in-current-directory