在 Emacs 中,我recentf
广泛使用。find-files
我通常不调用 ,而是调用自定义函数xsteve-ido-choose-from-recentf,它允许我从recentf
文件中进行选择。
如何创建和维护一个单独的最近目录列表,与最近文件列表分开?所以dired
我可以打电话而不是打电话,比如ido-choose-from-recent-directories
?
在 Emacs 中,我recentf
广泛使用。find-files
我通常不调用 ,而是调用自定义函数xsteve-ido-choose-from-recentf,它允许我从recentf
文件中进行选择。
如何创建和维护一个单独的最近目录列表,与最近文件列表分开?所以dired
我可以打电话而不是打电话,比如ido-choose-from-recent-directories
?
您在回复@Stefan 的回答的评论中问:我如何从上面查看最近的目录列表?-
答案是您使用了一个鲜为人知的事实,即如果DIRNAME
参数dired
是 (a) 新 Dired 缓冲区名称后跟 (b) 文件(或目录)名称的列表,那么 Dired 仅针对这些文件/目录打开. 爱荷华州:
M-: (dired (cons DIRED-BUFNAME YOUR-LIST-OF-RECENT-DIRECTORIES))
例如:
M-: (dired '("My Dired Buffer" "/a/recent/dir/" "/another/recent1/" "/another/"))
如果您使用库Dired+,那么您可以通过使用带有 . 的非正前缀 arg 以交互方式提供这样的列表dired
。
但是在这种情况下,您要编写一个命令,该命令首先收集最近目录的列表,然后为它们打开 Dired。这应该这样做:
(defun dired-recent (buffer)
"Open Dired in BUFFER, showing the recently used directories."
(interactive "BDired buffer name: ")
(let ((dirs (delete-dups
(mapcar (lambda (f/d)
(if (file-directory-p f/d)
f/d
(file-name-directory f/d)))
recentf-list))))
(dired (cons (generate-new-buffer-name buffer) dirs))))
这对我行得通。但是,vanilla Emacs 不允许您使用与 Dired 缓冲区i
不在同一目录树中的任何目录插入列表。default-directory
这意味着上面的代码可以正常工作,但您将无法插入任何列出的目录。
为了能够做到这一点,请加载 library dired+.el
。Dired+还修复了其他一些不足之处,即 cons arg 到dired
.
上面的代码,连同Dired+应该会给你你想要的。
更新
我刚刚将此添加到Dired+。这些是添加的命令:diredp-dired-recent-dirs
和diredp-dired-recent-dirs-other-window
.
更新 2
我使选择包含或排除哪些最近使用的目录变得简单。使用前缀 arg 来启动此类选择。没有前缀 arg 你会得到所有最近的目录。我还可以使用前缀 arg 来提示ls
开关。这是 doc 字符串diredp-dired-recent-dirs
:
Open Dired in BUFFER, showing recently used directories.
You are prompted for BUFFER.
No prefix arg or a plain prefix arg (`C-u', `C-u C-u', etc.) means
list all of the recently used directories.
With a prefix arg:
* If 0, `-', or plain (`C-u') then you are prompted for the `ls'
switches to use.
* If not plain (`C-u') then:
* If >= 0 then the directories to include are read, one by one.
* If < 0 then the directories to exclude are read, one by one.
When entering directories to include or exclude, use `C-g' to end.
最后,我为命令添加了绑定:(C-x R
相同窗口)和C-x 4 R
(其他窗口),其中R
Shift + r
。
您不需要维护一个单独的列表(这将是很多工作)。相反,您可以从最近的列表中提取该列表。例如
(delete-dups
(mapcar (lambda (file)
(if (file-directory-p file) file (file-name-directory file)))
recentf-list))
Pragmatic Emacs找到了解决方案。
这是一个功能,可以为您提供最近的目录列表,使用 ivy(swiper 的一部分)动态缩小范围,然后在 dired 中打开选定的目录。
;; open recent directory, requires ivy (part of swiper)
;; borrows from http://stackoverflow.com/questions/23328037/in-emacs-how-to-maintain-a-list-of-recent-directories
(defun bjm/ivy-dired-recent-dirs ()
"Present a list of recently used directories and open the selected one in dired"
(interactive)
(let ((recent-dirs
(delete-dups
(mapcar (lambda (file)
(if (file-directory-p file) file (file-name-directory file)))
recentf-list))))
(let ((dir (ivy-read "Directory: "
recent-dirs
:re-builder #'ivy--regex
:sort nil
:initial-input nil)))
(dired dir))))
(global-set-key (kbd "C-x C-d") 'bjm/ivy-dired-recent-dirs)