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
In addition to other answers on the page, you can use
f.el
's functionf-touch
:M-:
(f-touch "__init__.py")
RETAfter this thread, Emacs has added two new commands:
These commands will be available in the emacs 27.1 release.
(shell-command (concat "touch " (buffer-file-name)))
will do what you want, if you've already opened the empty file.Programatically and without any dependency on
touch
, it's quite easy:You can mark an empty buffer as modified by running
set-buffer-modified-p
. Then when you save it, Emacs will write the file.The following works:
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.