How do I create an empty file in emacs?

2019-01-21 10:35发布

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

15条回答
孤傲高冷的网名
2楼-- · 2019-01-21 10:50

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

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

查看更多
Bombasti
3楼-- · 2019-01-21 10:55

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.

查看更多
Juvenile、少年°
4楼-- · 2019-01-21 10:59

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

查看更多
狗以群分
5楼-- · 2019-01-21 11:00

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

(with-temp-buffer (write-file "path/to/empty/file/"))
查看更多
Melony?
6楼-- · 2019-01-21 11:00

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
查看更多
可以哭但决不认输i
7楼-- · 2019-01-21 11:01

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.

查看更多
登录 后发表回答