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,
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.
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)))
(shell-command (concat "touch " (buffer-file-name)))
will do what you want, if you've already opened the empty file.
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
You can use the touch command:
M-! touch __init__.py RET
Use touch command.
M-! touch __init__.py RET