-- Leo's gemini proxy

-- Connecting to tilde.pink:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini;

Emacs recursive file and directory search with globstar


> Emacs is a pathway to many abilities some would consider unnatural.


"... put the warnings before the spell"


Feature is available in Emacs 28. Since commit 29bb72f0432c7b89d2f7dec5022c582f8e10ada9 to be exact.

Feature is handled by shell, not Emacs. It might be `bash`, `ksh` or other. Each shell behave slightly differently.

Emacs library `ls-lisp` will ruin it! After `ls-lisp` library is loaded feature is broken. Library is used by `tramp`. So after `tramp` is used `ls-lisp` is loaded and feature is broken. The pain is real. Be aware of that.


tldr


(setq dired-maybe-use-globstar t)    ; available since Emacs version 28
;; C-x d ~/path/**/lib*/**/main.*    ; example usage - search file
;; C-x d ~/path/**/dir-name/         ; example usage - search dir
;; (info "(emacs) Dired Enter")      ; eval for more info

Intro


Hello you Emacs wizards!


I observed that looking for information about recursive file searching in Emacs will give you mostly `find-name-dired` and list of possible fuzzy like packages with `projectile` as must have.


There is another, less known, way of recursive file and directory search that can be performed in vanilla Emacs. I'm talking about `globstar`. It's my prefer way of finding files and directories in large code bases and it requires only one line to configure.


What is globstar


Technically it's not an Emacs feature. It belongs to shells like `bash` and `ksh`. Thanks to dired mode we can use it inside Emacs. In simple words its double star `**` used in path patterns to match multiple or zero number of directories to find searched files or directories. If it's not clear by now read further and see examples below.


For your own research and better understanding of all possibilities I recommend to read man page about `glob` itself and then all sections of `bash` man page that mention `globastar`. I'm using bash as example but you might find this feature in different shells. Each shell might have slightly different behavior so please keep that in mind. If you also want to know what Emacs info page have to say about dired globstar support then read page "Dired Enter" in "Emacs" section.


$ man glob.7
$ man bash.1

man glob.7 page on the web

man bash.1 page on the web


(info "(emacs) Dired Enter")    ; eval this function to visit info page

Enabling globstar


;; Put this line in init file
(setq dired-maybe-use-globstar t)

;; Or use M-x customize-variable <RET> dired-maybe-use-globstar <RET>
;; and click around Customize Option buffer.  Value shold be non-nil.

Example usage


Example files structure:


$ tree
.
└── src
    ├── libs
    │   ├── big-lib
    │   │   ├── main.c
    │   │   └── main.h
    │   ├── lib1.h
    │   └── lib2.h
    ├── main.c
    ├── main.h
    └── tests
        └── main.c

Emacs dired commands:


C-x d **/*.h            ; find all header files
C-x d **/main.*         ; find all main files
C-x d **/lib*/**/*.h    ; find all header files inside libs dir
C-x d **/*-lib/         ; find all directories matching *-lib

Note that to search for directories you have to end pattern with `/`.


Each command produce dired buffer. This can be used not only for finding files but also to rename, copy, remove, move specific files across nested directory structures. This becomes extra handy if you are working in unknown big project that is not recognized as "project" by `project.el`, `projectile` and other similar libraries that provide convenient way of recursive searching for files and directories.


Have I mentioned that globstar is fast? Indeed it is.


EOF

-- Response ended

-- Page fetched on Sun May 19 15:41:31 2024