如何从dired模式创建新文件?

Kei*_*awa 50 emacs file dired

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

Rob*_*een 53

按Cx Cf. 这将提示输入文件名,使用当前缓冲区的当前目录作为放入它的目录.对于dired缓冲区,其当前目录就是您正在查看的目录.

  • 这对我不起作用.对我来说,当我做Cx,Cf时,我得到了find-file提示符.我输入新的文件名.我得到`文件不存在,创建缓冲区?`我选择`y`.我输入了一个新的BUFFER,其中包含我输入的文件名.但是,如果我将缓冲区留空,那么当我执行Cx Cw时,它会提示我再次输入文件名,然后我回到我开始的位置.换句话说,此方法强制我输入两次文件名.假设我只想创建文件但不输入任何文本,有没有办法解决这个问题? (2认同)
  • @incandescentman:因为emacs努力不创建空文件,Emacs确认你真的想创建空文件.所以这就是你需要详细确认的原因.为避免这种情况,您需要更改缓冲区.例如,键入"a"然后在缓冲区中删除它.或者[elisp here](http://stackoverflow.com/a/2592558/2931409)可能是你想要的. (2认同)

Dre*_*rew 27

如果你想c在Dired模式下做什么C-x C-f,答案是微不足道的:

(define-key dired-mode-map "c" 'find-file)
Run Code Online (Sandbox Code Playgroud)

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

(define-key dired-mode-map "c"
  (lambda () (interactive) (find-file "untitled.txt")))
Run Code Online (Sandbox Code Playgroud)


Kei*_*awa 9

谢谢大家,我终于自己解决了.是我的答案.在dired模式下键入"c"将提示您创建新的无标题文件.然后按Enter将创建新的无标题文件.是的,这是非常详细的代码.有人可能会解决它.

(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))
       )
     )
  )
Run Code Online (Sandbox Code Playgroud)


use*_*626 7

  1. 按Cx Cf
  2. 输入文件名
  3. 按Cj

完成上述步骤后,emacs将使用您输入的名称创建一个空缓冲区.但默认情况下,emacs不支持创建空文件.您可以参考如何在emacs中创建空文件以获取更多想法


law*_*ist 5

The following contains two (2) options, one of which requires that touch be in the $PATH -- alternatively, the absolute path may be used. touch is usually available on unix flavor systems, e.g., OSX, etc. This function will automatically number the files / buffers successively -- e.g., untitled.txt; untitled1.txt; untitled2.txt, etc.

(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.")) )) ))
Run Code Online (Sandbox Code Playgroud)