Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go to a file quickly in Emacs Dired?

Tags:

emacs

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.

like image 210
szli Avatar asked Dec 13 '13 03:12

szli


People also ask

What is the command to open a file from within Emacs?

Use Ctrl-x f to open a file from within Emacs. Create a new file in the same way as opening a file by specifying the new filename.

How do I move files in dired?

Open dired for a directory containing files you want to work with. Then use C , R , or D when the cursor is on the line of a file to copy, rename/move or delete the file, respectively. This can also be done for multiple files by marking them.

How do you use dired?

You can start Dired Mode with C-x d , which will then prompt you to enter the directory you wish to view. You can also enter Dired with C-x C-f and then typing the directory you wish to view (as opposed to entering a filename). Dired Mode is a read-only buffer, so many of the commands are single letter keystrokes.

What is dired in Emacs?

Dired is the main mode for Emacs file-manager operations. The name “Dired” stands for “directory editor”. A single Dired buffer can display the contents of a single directory, or it can include listings of one or more sub-directories.


2 Answers

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.

like image 107
Tobias Avatar answered Sep 22 '22 08:09

Tobias


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.

like image 42
jl8e Avatar answered Sep 20 '22 08:09

jl8e