简体   繁体   English

如何在Emacs Dired中快速找到文件?

[英]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. 我知道在远程文件管理器中,人们可以按Alt键并开始键入名称,光标将在您键入时移动,我想知道Emacs是否有类似的东西。

The fastest way to answer this question is copying a section from the emacs info files. 回答这个问题的最快方法是从emacs信息文件中复制一个部分。 To get to this text type Ch i d m emacs ret and then isearch for an appropriate substring ignoring the first Failing I-search or just go to info with Ch i and then directly go to the info node mentioned below with g and then typing the node name followed by ret . 为了得到这个文本类型CH 1 D M emacs 退役 ,然后isearch一个合适的子忽略第一个Failing I-search或者只是去到的信息与 ,然后直接去下面相提到的信息节点,然后键入节点名称后跟ret

The first from the info-node (emacs) Dired and Find works whether one is in a dired buffer or not: 信息节点(emacs) Dired and Find无论是否在一个直接缓冲区中都有效:

   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. 来自info-node (emacs) Dired Navigation的第二个(emacs) Dired Navigation在dired缓冲区中工作,但仅适用于当前列出的文件。 But, note that you can list subdirectories with i before. 但是,请注意,您可以使用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.

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. 您可以通过使用prefix-arg调用dired并将R添加到开关列表来递归列出子目录。

The following snippet for the emacs initialization file would even simplify this task: 以下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))

With this snippet you get a normal listing of directories if you call find-file ( Cx Cf ) 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. 使用此代码段,如果为末尾没有斜杠的目录调用find-file( Cx Cf ),则会获得正常的目录列表,如果在末尾使用斜杠调用它,则会获得递归列表。 But be careful. 不过要小心。 Recursive directory listing can take its time. 递归目录列表可以花时间。 If you get nervous you can always quit with Cg . 如果你感到紧张,你可以随时退出Cg

isearch will do that. isearch会这样做。 control-s, then start typing. control-s,然后开始输入。

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. 由于它是emacs的通用搜索功能,它可以开始匹配缓冲区中的其他内容,直到你得到足够的名称。

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. 显然它只适用于一个文件,它会打开它,而不是提供可以对文件执行的大量操作,但至少它很快。

@Tobias and @jl8e have both answered your question directly: use Cs (incremental search). @Tobias和@ jl8e都直接回答了你的问题:使用Cs (增量搜索)。 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 (eg same prefix), then you can use %m to mark all files matching a regexp. 但是,如果您要多次使用所列文件的子集,或者您要对文件的子集执行相同的操作,并且可以通过文件名模式识别该子集(例如,相同的前缀) ),然后您可以使用%m来标记与正则表达式匹配的所有文件。 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. Dired主要是关于检查和操作文件 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. 单例集是一种特殊情况,对标记文件进行操作的所有操作(键)也仅在当前行的文件上运行(如果没有标记)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM