如何在dired模式下创建新文件?

63

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


13
我不想输入C-x C-f,我想要更简单的方法。 - Kei Minagawa
2
如果 untitled.txt 已经存在会发生什么?相信我,这个问题已经解决了,C-x C-f 是最简单的方法。 - abo-abo
1
应该创建"untitled2.txt"。 - Kei Minagawa
5
我已经使用这个函数几周了。我发现我需要先删除“untitled.txt”,然后再输入新的文件名。删除操作浪费时间,所以最终我按照你说的使用了C-x C-f。感谢abo-abo。 - Kei Minagawa
4
@abo-abo,这并不是愚蠢的想法。当我只是想创建一个文件(而不是切换到它),为什么我要按下两个预设键来切换到一个不需要的缓冲区呢?尤其是在一个专门用于单键目录功能的模式中。 "dired" 中应该有一个类似于 touch 的 analong,承认一下吧。 - Dodgie
显示剩余3条评论
6个回答

62

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


3
对我来说这行不通。当我按下C-x,C-f时,我会得到“查找文件”提示。我输入新的文件名。我得到“文件不存在,创建缓冲区?”提示,我选择“y”。然后我会得到一个名为我所输入的文件名的新缓冲区。但是,如果我将缓冲区留空,那么当我按下C-x C-w时,它会再次提示我输入文件名,而我又回到了起点。换句话说,这种方法强制我输入两次文件名。假设我只想创建文件但不输入任何文本,有没有什么方法避免这种情况? - incandescentman
1
M-x shell,然后在shell中输入touch filename。这仅适用于非Windows系统,并且仅适用于您尚未在Emacs中打开shell的情况。 - Robin Green
2
@incandescentman: 因为Emacs不希望创建空文件,所以它会确认你是否真的想要创建空文件。这就是为什么你需要冗长的确认操作。要避免这种情况,你需要切换buffer。例如,输入"a"然后在buffer中删除它。或者使用此处的elisp也可以达到你的目的。 - Kei Minagawa

33

如果您希望在Dired模式下使用c来执行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")))

1
我不得不用(with-eval-after-load 'dired ...)将其包装起来。 - Jay
是的,我也一样。如果不包含在代码块中,你会得到一个错误信息——“符号作为变量时未定义:dired-mode-map”。 - Kirill Yunussov

11

感谢大家,我终于自己解决了问题这里是我的答案。在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))
       )
     )
  )

如何修改此代码以避免默认为"untitled.txt"? - user129393192

11
  1. 按下 C-x C-f
  2. 输入文件名
  3. 按下 C-j

完成以上步骤后,Emacs 将创建一个以你输入的名称命名的空缓冲区。但是 Emacs 默认不支持创建空文件。你可以参考如何在 Emacs 中创建空文件获取更多想法。


5
下面包含两个选项,其中一个要求 touch$PATH 中 - 或者可以使用绝对路径。 touch 通常在Unix系统中可用,例如OSX等。此函数将自动按顺序编号文件/缓冲区,例如 untitled.txt; untitled1.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小技巧。谢谢。 - Kei Minagawa

3
如果您想输入文件名,则我认为这是一个重复的问题: 如何在emacs中创建一个空文件? 如果您不想输入文件名,仍然可以使用其中一些答案,并通过硬编码名称而不是交互式提示轻松地调整其他答案。

是的!就是这样。你的代码非常有帮助。我使用了你代码中的一些函数。非常感谢你。 - Kei Minagawa
这是唯一一个真正按照我想要的方式运作的解决方案。 - incandescentman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接