How to create new file from dired mode?

前端 未结 6 1265
眼角桃花
眼角桃花 2021-02-06 20:52

I want to create a new file in dired mode. Is there \"create new file\" command in dired mode ? For example, When I type \"c\" in dired mode, it creates \"untitled.txt\". It\'s

6条回答
  •  北荒
    北荒 (楼主)
    2021-02-06 21:02

    Thanks to all, I finally solved it myself. Here is my answer. Typing "c" in dired mode will prompt you creating new untitled file. Then press enter will create new untitled file. Yes it's very verbose code. Someone may fix it.

    (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))
           )
         )
      )
    

提交回复
热议问题