szli
szli

Reputation: 39119

How to go to a file quickly in Emacs Dired?

If a directory contains many files and I want to go to files(or subdirectories) with name starting with a letter (or a string), is there any good way to do it? I know in Far File Manager people can press Alt and start typing the name and the cursor will move as you type, I am wondering if Emacs has something similar.

Upvotes: 7

Views: 2398

Answers (4)

Tobias
Tobias

Reputation: 5198

The fastest way to answer this question is copying a section from the emacs info files. To get to this text type C-h i d m emacs ret and then isearch for an appropriate substring ignoring the first Failing I-search or just go to info with C-h i and then directly go to the info node mentioned below with g and then typing the node name followed by ret.

The first from the info-node (emacs) Dired and Find works whether one is in a dired buffer or not:

   To search for files with names matching a wildcard pattern use `M-x
find-name-dired'.  It reads arguments DIRECTORY and PATTERN, and
chooses all the files in DIRECTORY or its subdirectories whose
individual names match PATTERN.

The second from the info-node (emacs) Dired Navigation works in dired buffers but only applies to the currently listed files. But, note that you can list subdirectories with i before.

   `M-s f C-s' (`dired-isearch-filenames') performs a forward
incremental search in the Dired buffer, looking for matches only
amongst the file names and ignoring the rest of the text in the buffer.
`M-s f M-C-s' (`dired-isearch-filenames-regexp') does the same, using a
regular expression search.  If you change the variable
`dired-isearch-filenames' to `t', then the usual search commands also
limit themselves to the file names; for instance, `C-s' behaves like
`M-s f C-s'.  If the value is `dwim', then search commands match the
file names only when point was on a file name initially.  *Note
Search::, for information about incremental search.

EDIT: To isearch a directory recursively you may first list it recursively.

You can list sub-directories recursively by calling dired with prefix-arg and adding R to the list of switches.

The following snippet for the emacs initialization file would even simplify this task:

(defun dired-noselect-maybe-recursive (dir-or-list &optional switches)
  "Like `dired-noselect' but lists sub-directories when last character is ?/."
  (if (and (null switches)
       (= (aref dir-or-list (1- (length dir-or-list))) ?/))
      (dired-noselect dir-or-list "-alR")
    (dired-noselect dir-or-list switches)
    ))

(setq find-directory-functions (subst 'dired-noselect-maybe-recursive 'dired-noselect find-directory-functions))

With this snippet you get a normal listing of directories if you call find-file (C-x C-f) for a directory without a slash at the end and you get a recursive listing if you call it with a slash at the end. But be careful. Recursive directory listing can take its time. If you get nervous you can always quit with C-g.

Upvotes: 6

abo-abo
abo-abo

Reputation: 20362

I just want to add a trick that I like to use that's somewhat related to your question:

(add-hook
 'dired-mode-hook
 (lambda()
   (define-key dired-mode-map "j" 'ido-find-file)))

Obviously it works for just one file, and it opens it, instead of offering a multitude of things that dired can do to a file, but at least it's fast.

Upvotes: 3

Drew
Drew

Reputation: 30708

@Tobias and @jl8e have both answered your question directly: use C-s (incremental search). Nothing to add to that.

But if you are going to be using a subset of the listed files multiple times, or you are going to perform the same operation on a subset of the files, and if that subset can be identified by a file-name pattern (e.g. same prefix), then you can use %m to mark all files matching a regexp. Once marked, you can do all kinds of things on the set of files marked (or on the set of those unmarked).

(One of the things you can do is just omit the marked or unmarked file names from the listing, and hence from operations.)

Dired is really mostly about examining and operating on sets of files. The singleton set is a special case, and all operations (keys) that operate on the marked files also operate on the current line's file only, if none are marked.

Upvotes: 2

jl8e
jl8e

Reputation: 151

isearch will do that. control-s, then start typing.

Since it's emacs' general-purpose search function, it could start out matching other things in the buffer until you get enough of the name in.

Upvotes: 4

Related Questions