incandescentman
incandescentman

Reputation: 6388

in Emacs, how to search filenames in fixed set of directories?

In Emacs, is there a mode or function that will allow me to search for a string within the filenames (but not within the files) in a fixed set of non-contiguous directories that I have specified in advance?

I believe projectile-mode allows me to search for filenames within a project, but I believe a project is defined as a directory containing files. I would need to search in several disparate directories.

Upvotes: 3

Views: 721

Answers (2)

event_jr
event_jr

Reputation: 17707

If you're open to using helm, I made helm-cmd-t specifically for this purpose. Open issues on github if it's not clear enough how to use it.

Upvotes: 2

Drew
Drew

Reputation: 30708

Library Icicles can help with this.

  1. In Icicle mode, all commands, including file-finding (visiting) commands, let you act on multiple input patterns at the same time. For example, to visit all files in directory /a/b/c whose names match Unix/Linux glob pattern foo*.el, plus all files in dir /x/y whose names match glob pattern bar*toto*.c, you can do this:

     C-x C-f /a/b/c/foo*.el /x/y/bar*toto*.c M-R
    

    The M-R is what causes your minibuffer input to be parsed as multiple file-name patterns. (If you need a pattern that matches file names with spaces then enclose the pattern with "...".)

    See this page, which describes this simple multi-input feature.

  2. You can create a set of file names to reuse later, including persistently (i.e., reuse in another Emacs session). This page describes this feature.

    To do this, after C-x C-f you type a file-name pattern that matches some of the files you want (e.g. in a particular directory), then hit TAB or S-TAB to complete the pattern (and show the matches in popup window *Completions*), and then you hit C-M-> (or C-u C-M-> to save persistently).

    The patterns here are not glob patterns -- you are not passing them to the shell. They are Emacs Lisp regexps, or substring patterns, or prefix patterns, or fuzzy-matching patterns,... In general, regexps are your friends here.

    Using C-M-> marks or saves the list of matching file names. You can save to a Lisp variable or a file, or just to memory using no variable.

    Now change your minibuffer input (you are still in the same C-x C-f command invocation) to a different dir and file-names pattern, and then hit C-> to add the new matches to the saved set you created using the first pattern. Repeat for as many dirs and file patterns as you like. End with C-g if you don't want to visit the files now, or with RET if you do.

    To reuse a saved set of file names, use C-x C-f and then hit C-M-<. (To retrieve a persistently saved set, use C-u first: C-u C-M-<.) What this does is define the current set of file-name matches to be those in your saved set of names. Hit RET to visit all of them. Or you can add to or subtract from the set, to open a slightly different set.

    During completion you can also combine sets of candidates: complementing, union, intersection, set difference. There are quick keys bound for such operations.

    You can also define a set for later reuse using Lisp. An easy way to do this interactively is to use M-x icicle-candidate-set-define during file-name completion. It prompts you for a Lisp sexp that evaluates to a list of file-name strings.

  3. Yet another way: In a Dired buffer, mark the files you want there, then use C-M-> to save them as a set that you can reuse as in #2, above. Go to another Dired directory, mark files there, and then use C-> to add them to the set you defined from the first Dired buffer. And so on.

    You can even collect all of the marked files from a Dired buffer and the marked files in its marked subdirs (or all of the files in its marked subdirs), by using M-+ C-M-> (and similarly, for M-+ C->).

Besides saving a set of file names in a file, as suggested above, you can use an Emacs bookmark. If you use library Bookmark+ then you can easily bookmark sets of files and directories using Dired bookmarks (marks are remembered etc.). You can also tag files and directories (as bookmarks) with any number of free-form tags. Each tag defines, in effect, a set of bookmarks. See the Bookmark+ doc for more info about these features.

Though it is a separate library from Bookmark+ (neither requires the other), Icicles offers many ways to make use of such Bookmark+ features, including easily accessing files and dirs that are tagged with particular sets of tags.

This should be enough to get you started.

Upvotes: 2

Related Questions