【问题标题】:Create a directory by editing通过编辑创建目录
【发布时间】:2014-05-03 12:48:51
【问题描述】:

当我在 Emacs dirmode 中键入 r 时,它会进入一个允许通过编辑列表进行目录操作(例如重命名、移动、删除)的模式。

我有一个特定的文件,比如foo,我想在它的原始目录下创建一个新目录,将该目录命名为foo,然后将文件移动到该目录中,并给它一个不同的名称,比如说bar

/path/to/the/file/foo
↓
/path/to/the/file/foo/bar

我在此模式下编辑了列表,并保存了,但操作失败。看起来像这样的编辑不允许创建目录。有什么技巧可以让这成为可能吗?

我有很多类似的任务要做,在 emacs dirmode 上编辑对我来说比编写 shell 脚本更容易。

【问题讨论】:

  • 听起来像是添加到 wdired 的一个非常有用的功能,所以我建议您 M-x report-emacs-bug 询问这个新功能。

标签: emacs dir


【解决方案1】:

您可以按如下方式覆盖 wdired-do-renames 的实现(想法只是在重命名之前创建每个目标文件的父目录):

diff --git a/lisp/wdired.el b/lisp/wdired.el
index 2e53944..cc838cf 100644
--- a/lisp/wdired.el
+++ b/lisp/wdired.el
@@ -490,6 +490,7 @@ non-nil means return old filename."
               (require 'dired-aux)
               (condition-case err
                   (let ((dired-backup-overwrite nil))
+                    (wdired-create-parentdirs file-new)
                     (dired-rename-file file-ori file-new
                                        overwrite))
                 (error
@@ -499,6 +500,10 @@ non-nil means return old filename."
                             err)))))))))
     errors))

+(defun wdired-create-parentdirs (file-new)
+  "Create parent directories of destination filename."
+  (message "create dir for file %s" file-new)
+  (make-directory (file-name-directory file-new) t))

 (defun wdired-exit ()
   "Exit wdired and return to dired mode.

来源:Joakim Verona via http://www.emacswiki.org/emacs/WDired#toc1(我稍微清理了补丁)

【讨论】:

    【解决方案2】:

    AFAIK,您不能以这种方式创建目录,即在 wdired 模式下(当 Dired 缓冲区可编辑时您所处的状态)。

    但是,您可以退出wdired (C-x C-q),然后使用+ 创建目录。

    (顺便说一句,我认为您有一个错字:Dired 中的C-r 不会切换wdired 模式,即可编辑性/只读模式。它只是调用isearch-backward。您使用C-x C-q 来切换读取-仅限。)

    【讨论】:

      猜你喜欢
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      相关资源
      最近更新 更多