可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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))))))
回答2:
You can use the touch command:
M-! touch __init__.py RET
回答3:
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.
回答4:
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)))
回答5:
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.
回答6:
Programatically and without any dependency on touch
, it's quite easy:
(with-temp-buffer (write-file "path/to/empty/file/"))
回答7:
Use touch command.
M-! touch __init__.py RET
回答8:
The shortest way
M-! > __init__.py
RET
回答9:
After this thread, Emacs has added two new commands:
- make-empty-file
- dired-create-empty-file
These commands will be available in the emacs 27.1 release.
回答10:
(shell-command (concat "touch " (buffer-file-name)))
will do what you want, if you've already opened the empty file.
回答11:
In addition to other answers on the page, you can use f.el
's function f-touch
:
M-:(f-touch "__init__.py")
RET
回答12:
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
回答13:
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))
回答14:
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.
回答15:
The best option would be:
(with-temp-file "filename"
(insert ""))