如何在Emacs Dired中快速进入文件?

7

如果一个目录包含许多文件,我想要进入以某个字母(或字符串)开头的文件(或子目录),有没有好的方法可以做到这一点?我知道在Far文件管理器中,人们可以按下Alt键并开始输入名称,光标会随着您的输入而移动,我想知道Emacs是否有类似的功能。


在我的回答下面,我添加了一个关于递归搜索目录的注释。 - Tobias
如果你想快速在子目录中查找文件(即递归),你应该看看 projectile 及其 find-file-in-projectfind-file-in-directory 函数:https://github.com/bbatsov/projectile - Ehvince
然后还有瑞士军刀:在http://www.emacswiki.org/emacs/TN的“无需依赖外部程序的Grepping”一节中。如果您使用前缀键调用`elgrep`,它将递归搜索,如果您仅指定文件名的正则表达式并将其他正则表达式留空,则只会搜索具有此类文件名的文件。 - Tobias
4个回答

6
这个问题的最快解答方式是从emacs信息文件中复制一部分。要获取此文本,请键入C-h i d m emacs ret,然后使用isearch查找适当的子字符串(忽略第一个Failing I-search),或者直接用C-h i进入info,然后用g导航到下面提到的info节点并输入节点名称,最后按ret即可。
从info节点(emacs) Dired and Find开始的方法无论是否在dired缓冲区中都可以使用:
   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.

来自信息节点 (emacs) Dired Navigation 的第二个方法适用于dired缓冲区,但仅适用于当前列出的文件。但请注意,在使用 i 命令之前,您可以列出子目录。

   `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.

编辑: 要递归地搜索目录,您可以先递归列出它。

您可以通过使用前缀参数调用dired并将R添加到开关列表中来递归地列出子目录。

以下用于Emacs初始化文件的代码片段甚至可以简化此任务:

(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))

如果您在调用没有斜杠结尾的目录时使用find-file(C-x C-f),则可以获得普通的目录列表,如果在结尾处使用斜杠,则可以获得递归列表。

但是要小心。递归目录列表可能需要一些时间。如果感到紧张,您可以随时使用C-g退出。


4

isearch可以实现这一点。按Control-S,然后开始输入。

由于它是Emacs的通用搜索功能,因此在输入名称的足够信息之前,它可能会匹配缓冲区中的其他内容。


3
我想补充一个我喜欢使用的技巧,与你的问题有些相关:
(add-hook
 'dired-mode-hook
 (lambda()
   (define-key dired-mode-map "j" 'ido-find-file)))

显然,这只适用于一个文件,并且它会打开该文件,而不是提供dired可以对文件执行的多种操作,但至少它很快。


2
@Tobias和@jl8e已经直接回答了你的问题:使用C-s(增量搜索)。没有其他需要补充的。
但是,如果您将多次使用列出的文件的子集,或者您将在文件的子集上执行相同的操作,并且如果该子集可以通过文件名模式(例如相同的前缀)进行识别,则可以使用%m标记与正则表达式匹配的所有文件。一旦标记,您可以对标记的文件集合(或未标记的文件集合)执行各种操作。
(其中一件事情就是仅从列表中省略标记或未标记的文件名,因此也省略了操作。)
Dired实际上主要是关于检查和操作文件集合。单例集合是一种特殊情况,如果没有标记任何文件,则所有操作(键)都仅对当前行的文件进行操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接