💾 Archived View for irek.xyz › articles › emacs-globstar.gmi captured on 2022-03-01 at 15:16:40. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-11-30)

🚧 View Differences

-=-=-=-=-=-=-

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"

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