How to create new file from dired mode?
Asked Answered
R

6

64

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 very simple , but I can't find it.

Reduction answered 30/11, 2013 at 12:18 Comment(8)
I don't want to type C-x C-f. I want more simple way.Reduction
What happens if untitled.txt already exists? Just trust me that this problem is solved and C-x C-f is the simplest wayFroemming
"untitled2.txt" should be created.Reduction
This is why create new file command is not implmented. But I want to complete my file related operation in dired mode.Reduction
I used this function for a few weeks. And I noticed that I erase "untitled.txt" and then input new file name. Erasing is a waste of time. So eventually I am using C-x C-f as you said. Thanks abo-abo.Reduction
@Froemming it's not that silly. Why should I have to press two prefixed keys that will switch me to an unwanted buffer, when all I want is to create a file (an not switch to it). In a mode dedicated to single-key directory functions, no less. There should be a touch analog in "dired", admit itKissner
I find it astounding that a directory editor (dired)/file-manager does not have a built in means to create empty files.Mcclinton
Is there an answer to this question now? If I want a touch analog in dired?Fender
N
62

Just press C-x C-f. This will prompt for a filename, using the current directory of the current buffer as the directory to put it in. For a dired buffer, its current directory is simply the directory you are looking at.

Nita answered 30/11, 2013 at 12:22 Comment(3)
This doesn't work for me. For me, when I do C-x, C-f, I get the find-file prompt. I enter the new filename. I get File does not exist, create buffer? I select y. I get a new BUFFER with the filename I entered. However, if I leave the buffer empty, then when I do C-x C-w, it prompts me AGAIN to enter a filename and I'm back where I started. In other words, this method forces me to enter the filename twice. Assuming I just want to create the file but not enter any text, is there a way around this?Daphinedaphna
M-x shell and in the shell type touch filename. This only works on non-Windows systems and only if you don't already have a shell open inside Emacs.Nita
@incandescentman: Because emacs efforts not to create empty file, Emacs confirm you really want to create empty file. So that's why you need verbose confirmation. To avoid this you need to change buffer. For example, type "a" then delete it on the buffer. Or elisp here may what you want.Reduction
S
32

If you want c in Dired mode to do what C-x C-f does, the answer is trivial:

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

Or if you want it to have the name untitled.txt then:

(define-key dired-mode-map "c"
  (lambda () (interactive) (find-file "untitled.txt")))
Subsume answered 30/11, 2013 at 17:28 Comment(2)
I had to wrap it in (with-eval-after-load 'dired ...)Kreisler
yep, same here. if not wrapped, you get an error - "Symbol's value as variable is void: dired-mode-map"Laceration
B
12
  1. press C-x C-f
  2. enter file name
  3. press C-j

after the above steps, emacs will create an empty buffer with the name you input. But emacs doesn't support to create empty file by default. You can refer to How do I create an empty file in emacs for more ideas

Ballinger answered 6/12, 2016 at 8:39 Comment(0)
R
11

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))
       )
     )
  )
Reduction answered 1/12, 2013 at 8:24 Comment(1)
How could this code be editted to not have it "untitled.txt" by default?Anastassia
H
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.")) )) ))
Handpick answered 30/11, 2013 at 16:22 Comment(1)
Now I can understand what you mean. I can learn a lot of emacs useful tips from this code. Thanks.Reduction
S
3

If you want to enter the filename, then I think this is a duplicate of:

How do I create an empty file in emacs?

If you don't want to enter the filename, you could still use some of those answers, and easily adapt others by hard-coding the name rather than prompting for it interactively.

Stopover answered 30/11, 2013 at 13:31 Comment(2)
Yes!! That's it. Your code is very helpful. I used some of the functions in your code. Thank you very much.Reduction
This is the only solution that actually worked the way I wanted it to.Daphinedaphna

© 2022 - 2024 — McMap. All rights reserved.