【问题标题】:How to create new file from dired mode?如何从 dired 模式创建新文件?
【发布时间】:2013-11-30 12:18:38
【问题描述】:

我想在 dired 模式下创建一个新文件。在 dired 模式下是否有“创建新文件”命令?例如,当我在 dired 模式下键入“c”时,它会创建“untitled.txt”。很简单,就是找不到。

【问题讨论】:

  • 我不想输入 C-x C-f。我想要更简单的方法。
  • 如果untitled.txt 已经存在会怎样?相信我,这个问题已经解决了,C-x C-f 是最简单的方法
  • "untitled2.txt" 应该被创建。
  • 这个功能我用了几个星期。我注意到我删除了“untitled.txt”,然后输入了新的文件名。擦除是浪费时间。所以最终我按照你所说的那样使用 C-x C-f 。谢谢 abo-abo。
  • @abo-abo 没那么傻。当我只想创建一个文件(而不是切换到它)时,为什么我必须按两个前缀键将我切换到不需要的缓冲区。在专用于单键目录功能的模式下,同样如此。 “dired”中应该有一个touch 模拟,承认它

标签: file emacs dired


【解决方案1】:

只需按 C-x C-f。这将提示输入文件名,使用当前缓冲区的当前目录作为放置它的目录。对于 dired 缓冲区,其当前目录就是您正在查看的目录。

【讨论】:

  • 这对我不起作用。对我来说,当我执行 C-x、C-f 时,我得到了查找文件提示。我输入新的文件名。我得到File does not exist, create buffer? 我选择y。我得到一个带有我输入的文件名的新 BUFFER。但是,如果我将缓冲区留空,那么当我执行 C-x C-w 时,它会再次提示我输入文件名,然后我又回到了开始的地方。换句话说,这种方法迫使我输入两次文件名。假设我只想创建文件但不输入任何文本,有没有办法解决这个问题?
  • M-x shell 并在 shell 中键入 touch filename。这仅适用于非 Windows 系统,并且仅当您尚未在 Emacs 中打开 shell 时。
  • @incandescentman:因为 emacs 努力不创建空文件,所以 Emacs 确认您确实要创建空文件。所以这就是你需要详细确认的原因。为避免这种情况,您需要更改缓冲区。例如,键入“a”然后将其从缓冲区中删除。或者elisp here 可能是你想要的。
【解决方案2】:

如果你想让 c 在 Dired 模式下做 C-x C-f 所做的事情,答案是微不足道

(define-key dired-mode-map "c" 'find-file)

或者,如果您希望它具有名称 untitled.txt,则:

(define-key dired-mode-map "c"
  (lambda () (interactive) (find-file "untitled.txt")))

【讨论】:

  • 我必须把它包裹在(with-eval-after-load 'dired ...)
  • 是的,这里也一样。如果没有包装,你会得到一个错误 - “符号作为变量的值是无效的:dired-mode-map”
【解决方案3】:

感谢大家,我终于自己解决了Here 是我的回答。在 dired 模式下键入“c”将提示您创建新的无标题文件。然后按回车键将创建新的无标题文件。是的,这是非常冗长的代码。有人可能会修复它。

(eval-after-load 'dired
  '(progn
     (define-key dired-mode-map (kbd "c") 'my-dired-create-file)
     (defun create-new-file (file-list)
       (defun exsitp-untitled-x (file-list cnt)
         (while (and (car file-list) (not (string= (car file-list) (concat "untitled" (number-to-string cnt) ".txt"))))
           (setq file-list (cdr file-list)))
         (car file-list))

       (defun exsitp-untitled (file-list)
         (while (and (car file-list) (not (string= (car file-list) "untitled.txt")))
           (setq file-list (cdr file-list)))
         (car file-list))

       (if (not (exsitp-untitled file-list))
           "untitled.txt"
         (let ((cnt 2))
           (while (exsitp-untitled-x file-list cnt)
             (setq cnt (1+ cnt)))
           (concat "untitled" (number-to-string cnt) ".txt")
           )
         )
       )
     (defun my-dired-create-file (file)
       (interactive
        (list (read-file-name "Create file: " (concat (dired-current-directory) (create-new-file (directory-files (dired-current-directory))))))
        )
       (write-region "" nil (expand-file-name file) t) 
       (dired-add-file file)
       (revert-buffer)
       (dired-goto-file (expand-file-name file))
       )
     )
  )

【讨论】:

    【解决方案4】:
    1. 按 C-x C-f
    2. 输入文件名
    3. 按 C-j

    在上述步骤之后,emacs 将使用您输入的名称创建一个空缓冲区。但是 emacs 默认不支持创建空文件。更多想法可以参考How do I create an empty file in emacs

    【讨论】:

      【解决方案5】:

      以下包含两 (2) 个选项,其中一个要求 touch$PATH 中——或者,可以使用绝对路径。 touch 通常在 unix 风格的系统上可用,例如 OSX 等。此功能将自动为文件/缓冲区连续编号 - 例如 untitled.txt;无标题1.txt; untitled2.txt等

      (define-key dired-mode-map (kbd "c") 'dired-new-file)
      
      (defun dired-new-file ()
      (interactive)
        (let* (
            (n 0)
            lawlist-filename
            (dired-buffer-name (buffer-name)))
          (catch 'done
            (while t
              (setq lawlist-filename (concat "untitled"
                (if (= n 0) "" (int-to-string n))
                  ".txt"))
              (setq n (1+ n))
              (if (not (file-exists-p lawlist-filename))
                (throw 'done nil)) ))
          (message "[b]uffer + file (maybe) | [f]ile + buffer (maybe)")
          (let ((file-or-buffer (read-char-exclusive)))
            (cond
              ((eq file-or-buffer ?b)
                (switch-to-buffer (get-buffer-create lawlist-filename))
                (text-mode)
                (or (y-or-n-p (format "Save Buffer `%s'? "lawlist-filename))
                  (error "Done."))
                (write-file lawlist-filename)
                (with-current-buffer dired-buffer-name
                  (revert-buffer)))
              ((eq file-or-buffer ?f)
                (start-process "touch-file" nil "touch" lawlist-filename)
                (revert-buffer)
                (or (y-or-n-p (format "Open `%s'? "lawlist-filename))
                  (error "Done."))
                (find-file lawlist-filename)
                (text-mode))
              (t (message "You have exited the function.")) )) ))
      

      【讨论】:

      • 现在我明白你的意思了。我可以从这段代码中学到很多 emacs 有用的技巧。谢谢。
      【解决方案6】:

      如果你想输入文件名,那么我认为这是重复的:

      How do I create an empty file in emacs?

      如果您不想输入文件名,您仍然可以使用其中一些答案,并通过对名称进行硬编码而不是交互式提示来轻松调整其他答案。

      【讨论】:

      • 是的!而已。你的代码很有帮助。我在你的代码中使用了一些函数。非常感谢。
      • 这是唯一真正按照我想要的方式工作的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      相关资源
      最近更新 更多