72

How can I create an empty file from emacs, ideally from within a dired buffer?

For example, I've just opened a Python module in dired mode, created a new directory, opened that in dired, and now need to add an empty __init__.py file in the directory.

If I use C-x C-f __init__.py RET C-x C-s then emacs doesn't create the file because no changes have been made to it. I would have to type in the file, save it, delete my typing and then save it again for that to work.

Thanks

Singletoned
  • 5,089
  • 3
  • 30
  • 32
  • 9
    I live with this deadlock: Emacs is a powerful text editor which can even integrate a Scala compiler, etc, etc ... but many times very simple things are overcomplicated. I find easier to "touch /path/to/filename" and later open it in Emacs than trying to create a new file inside Emacs, in particular when there's another file with the same name or even a similar name. Emacs "tries to help me" opening the wrong file. Please, do not "try to help me!"... just do what I told you that should be done! I'm pretty sure there must be a "plugin" (maybe dired) which does that. Overcomplicated! again! – Richard Gomes Jun 25 '15 at 10:03
  • 1
    @RichardGomes Having been using phils's solution for a couple of years now, I'm finding it much easier to just type "_" and it asks me for a file name. Emacs is definitely very complicated, but I don't think it's over-complicated. Much of what I've done to it over the last 5 years has been to make it more complicated, as well as shaving off all those annoying corners. I suffered from exactly the same problem with the completion as you, but it is fixable if you can be bothered to spend a few years getting to know the system. – Singletoned Jun 26 '15 at 10:24
  • 1
    `C-x C-f __init__.py RET C-x C-s` works fine for me (version `24.5.1`), so maybe the issue has been resolved since this question was originally asked. – Jian Jul 16 '15 at 06:17
  • You're right it does seem to have been fixed. And the nice thing is, it works anywhere, even when you aren't in dired. These days I've bound `C-x j` to open dired, and then `_` to create a new file, which is pretty short – Singletoned Jul 16 '15 at 08:15

15 Answers15

61

You can use the touch command:

M-! touch __init__.py RET
matthias krull
  • 4,389
  • 3
  • 34
  • 54
Dani
  • 14,639
  • 11
  • 62
  • 110
  • 1
    @offby1, if you add that as a proper answer, I'll choose it, as that's the one that works best for me. It even uses whatever directory dired is in as the cwd. – Singletoned Apr 07 '10 at 16:08
  • 1
    I like that very much, too! I just don't like typing the M-! bit, but I guess I can change the keybinding :) – Vivi Jun 08 '10 at 15:02
28

The following works:

C-x b __init__.py RET C-x C-w RET

If you're in a dired buffer the file will be saved in the directory show here.

The trick is to first create an empty buffer by switching to a name that doesn't exist. Then write out the file.

slu
  • 1,244
  • 9
  • 14
  • 1
    This method is great because it creates a new file, warns if it exists already and ends with the new file in an open buffer. This matches 99% of my use cases. – Timothy C. Quinn Mar 25 '14 at 17:49
  • In my set up, if the file name matches another one in its buffer history, then C-x b will open that file instead of new one. This is handy, except in cases where you want to create a new file, or you have multiple ones that have the same name (__init__.py would be one of those). – Jacob Lee May 11 '21 at 15:52
20

If you want Emacs to treat all new files as modified, you can automate the solution like this:

(add-hook 'find-file-hooks 'assume-new-is-modified)
(defun assume-new-is-modified ()
  (when (not (file-exists-p (buffer-file-name)))
    (set-buffer-modified-p t)))
Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
Kilian Foth
  • 13,904
  • 5
  • 39
  • 57
  • I like this, and I think I'll probably set it anyway, because it seems a saner in the general case, but the `M-! touch __init__.py` is shorter and doesn't involve opening and closing a buffer. – Singletoned Apr 07 '10 at 16:15
  • @Singletoned - you don't have to enter this every time. Just put it in your init file and every time you create a new file with `C-x C-f` it will show up in dired. That's quicker than typing out a shell command (`touch`) every time. – metakermit Dec 03 '13 at 13:45
  • What do you mean "treat as modified"? – user129393192 Jul 07 '23 at 18:22
14

Programatically and without any dependency on touch, it's quite easy:

(with-temp-buffer (write-file "path/to/empty/file/"))
joao
  • 3,517
  • 1
  • 31
  • 43
13

After this thread, Emacs has added two new commands:

  1. make-empty-file
  2. dired-create-empty-file

These commands will be available in the emacs 27.1 release.

Tino
  • 394
  • 4
  • 7
  • 1
    And that's what `make-empty-file` is using to make the empty file: `(write-region "" nil filename nil 0)` – Hubisan Jul 17 '19 at 21:27
  • Years (many years) ago emacs "New file" tab just did this: Open a window *without* necessity of a filename. "Nice" they got somehow back to this (better! imho) way. – Michael Dec 02 '19 at 13:42
12

Here's an adaptation of dired-create-directory. It works the same way, so as well as a plain filename, you can also specify new parent directories (to be created under the current directory) for the file (e.g. foo/bar/filename).

(eval-after-load 'dired
  '(progn
     (define-key dired-mode-map (kbd "C-c n") 'my-dired-create-file)
     (defun my-dired-create-file (file)
       "Create a file called FILE.
If FILE already exists, signal an error."
       (interactive
        (list (read-file-name "Create file: " (dired-current-directory))))
       (let* ((expanded (expand-file-name file))
              (try expanded)
              (dir (directory-file-name (file-name-directory expanded)))
              new)
         (if (file-exists-p expanded)
             (error "Cannot create file %s: file exists" expanded))
         ;; Find the topmost nonexistent parent dir (variable `new')
         (while (and try (not (file-exists-p try)) (not (equal new try)))
           (setq new try
                 try (directory-file-name (file-name-directory try))))
         (when (not (file-exists-p dir))
           (make-directory dir t))
         (write-region "" nil expanded t)
         (when new
           (dired-add-file new)
           (dired-move-to-filename))))))
phils
  • 71,335
  • 11
  • 153
  • 198
  • 3
    Although I've specified a user-reserved binding in the code, I've actually bound this to `_` which on my keyboard layout is the shifted sequence next to `+`, meaning the keys for "new directory" and "new file" in dired are right next to one another. – phils Mar 18 '14 at 11:13
  • I've changed this to be the answer, as it is the one that I'm using in the long term. – Singletoned Jan 12 '15 at 15:50
12

Emacs won't allow you to save a buffer unless it thinks the contents have changed. The quickest, though possibly not cleanest is to open the file using C-x C-f, then press (say) space and backspace, then you should be able to save a file with no contents.

There are other ways of changing the "buffer has been modified" flag, but I don't think there's any easier.

Vatine
  • 20,782
  • 4
  • 54
  • 70
6

Use touch command.

M-! touch __init__.py RET
Uchenna Nwanyanwu
  • 3,174
  • 3
  • 35
  • 59
5

The shortest way

Creates an empty file via a shell operation (but does not open it):

M-! > __init__.py RET

Open the new file:

C-x C-f RET

(Note: we don't have to type in the name again, because the new file is automatically the first choice)

music
  • 191
  • 5
thanhpk
  • 3,900
  • 4
  • 29
  • 36
  • That's quite clever, and definitely shorter than using touch. Though the solution I'm using is @phils which makes it just `_` followed by `__init__.py`. – Singletoned Dec 02 '16 at 08:23
3

(shell-command (concat "touch " (buffer-file-name))) will do what you want, if you've already opened the empty file.

Squidly
  • 2,707
  • 19
  • 43
2

In addition to other answers on the page, you can use f.el's function f-touch:

M-:(f-touch "__init__.py")RET

Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166
1

You can mark an empty buffer as modified by running set-buffer-modified-p. Then when you save it, Emacs will write the file.

M-;                         ; Eval
(set-buffer-modified-p t)   ; Mark modified
C-x C-s                     ; Save buffer
Ian Mackinnon
  • 13,381
  • 13
  • 51
  • 67
1

I use the following bound to t in dired.

(defun my-dired-touch (filename)
  (interactive (list (read-string "Filename: " ".gitkeep")))
  (with-temp-buffer
    (write-file filename)))

;; optionally bind it in dired
(with-eval-after-load 'dired
  (define-key dired-mode-map "t" 'my-dired-touch))
Rorschach
  • 31,301
  • 5
  • 78
  • 129
0

I've modified answer from MrBones and created custom function with keybinding:

; create empty __init__.py at the place
(defun create-empty-init-py()
  (interactive)
  (shell-command "touch __init__.py")
)
(global-set-key (kbd "C-c p i") 'create-empty-init-py)

This is very useful to not spend time on recurring action of creating init.py everywhere in new Python project folder.

alex_koval
  • 1,646
  • 1
  • 12
  • 5
0

The best option would be:

(with-temp-file "filename"
  (insert ""))