简体   繁体   English

如何在Emacs Dired的子目录中创建文件?

[英]How can I create a file in a sub-directory in Emacs Dired?

I use dired to browse a directory and its sub-directories with i ( dired-maybe-insert-subdir ) 我使用dired浏览带有i的目录及其子目录( dired-maybe-insert-subdir

When the cursor is in a sub-directory, and I do Cx Cf , the default directory is the root of my buffer, is there a way to change that? 当光标位于子目录中并且我执行Cx Cf ,默认目录是缓冲区的根目录,是否可以更改该目录?

So if I visit /foo/bar and then I insert in bar's buffer the content of /foo/bar/baz and have my cursor there, get a mini-buffer with /foo/bar/baz/ when I ask to visit a file (with the default keybinding or another, that doesn't matter to me). 因此,如果我访问/ foo / bar,然后在bar的缓冲区中插入/ foo / bar / baz的内容并将光标放在此处,则当我要求访问文件时,使用/foo/bar/baz/获得一个迷你缓冲区(使用默认的快捷键绑定或其他快捷键,这对我来说无关紧要)。

The following is my own solution. 以下是我自己的解决方案。

(defun dired-subdir-aware (orig-fun &rest args)
  (if (eq major-mode 'dired-mode)
      (let ((default-directory (dired-current-directory)))
        (apply orig-fun args))
    (apply orig-fun args)))

(advice-add 'find-file-read-args :around 'dired-subdir-aware)

I just learned how to use the advice function to augment existing functions (and macros). 我刚刚学习了如何使用建议功能来扩展现有功能(和宏)。 See (elisp) Advising Functions . 请参阅(省略号)建议功能

Now if you go to a subdir under dired and run Cx Cf , you will be prompted with a correct path. 现在,如果您转到dired下的子目录并运行Cx Cf ,将提示您输入正确的路径。

Update : 更新

Recently, I began to play with (ido) Top . 最近,我开始和(ido)Top一起玩。 With dired-subdir-aware defined, I easily extend ido-find-file to recognize subdir with the following code. 在定义了dired-subdir-aware ,我可以使用以下代码轻松扩展ido-find-file以识别subdir。

(dolist (fun '(find-file-read-args ido-expand-directory))
  (advice-add fun :around 'dired-subdir-aware))

No, default-directory is local to a buffer, not just part of a buffer. 不, default-directory是缓冲区本地的,而不仅仅是缓冲区的一部分。 This is by design, and there is no way to change it. 这是设计使然,无法更改它。

But you could of course define a command that picks up the subdir directory and then binds default-directory to that while it reads a file name to visit, etc.). 但是您当然可以定义一个命令,该命令选择subdir目录,然后在读取文件名进行访问时将default-directory绑定到该default-directory ,等等。

For example, this command will read a file name with the default directory being what you want, and then visit that file: 例如,此命令将读取一个文件名,默认目录为您想要的文件名,然后访问该文件:

(defun foo (file)
  "..."
  (interactive
   (let ((default-directory  (dired-current-directory)))
     (list (read-file-name "File: "))))
  (find-file file))

And, out of the box, you can certainly visit the subdir in another Dired buffer, where default-directory is what you want. 而且,开箱即用,您当然可以在另一个Dired缓冲区中访问子目录,该default-directory是您想要的default-directory Commands such as dired-do-find-marked-files ( F ) and dired-display-file ( Co ) do that. 诸如dired-do-find-marked-filesF )和dired-display-fileCo )之类的命令可以做到这一点。

But why do you want default-directory to reflect the subdir whose listing you are in? 但是,为什么要让default-directory反映您所在列表default-directory What's your use case? 您的用例是什么?

I refined Drew's answer to a function that does exactly what I wanted: 我完善了Drew对一个功能的回答 ,该功能完全满足我的要求:

(defun dired-find-file ()
  "Find a file in the current dired directory"
  (interactive)
  (let ((default-directory (dired-current-directory)))
    (find-file (read-file-name "Find file:"))))

I then bound this function to a convenient key combination: 然后,我将此功能绑定到方便的组合键:

(add-hook 'dired-mode-hook
   (lambda ()
     (local-set-key (kbd "C-x M-f") 'dired-find-file)))

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

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