💾 Archived View for irek.xyz › articles › emacs-globstar.gmi captured on 2021-11-30 at 20:18:30. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
Emacs is a pathway to many abilities some would consider unnatural.
(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
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.
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
(info "(emacs) Dired Enter") ; eval this function to visit info page
;; 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 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