I found this answer:

(eval-after-load 'dired
     (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))
         (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)
        (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)
       (dired-goto-file (expand-file-name file))

here. It does what I like, which is that pressing c will create a new file in dired mode, but what I don't like is that it's by default named untitled.txt. For example, if I press c in dired with this inside ~/.emacs.d I get Create file: ~/.emacs.d/untitled.text, but what I want is Create file: ~/.emacs.d/ so I can pick the name myself. How can I accomplish this?

  • 1
    C-h f read-file-name, read the doc string and decide what behavior you want: in particular, you'll need to think about what to do if the user just presses RET at that point without entering a name.
    – NickD
    Jul 7, 2023 at 18:45
  • Isn't that an issue even here if they get rid of untitled.txt @NickD ? Jul 7, 2023 at 20:25


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.