如何在 emacs 中创建空文件?

发布于 2024-08-28 03:51:18 字数 258 浏览 10 评论 0原文

如何从 emacs 创建一个空文件,最好是在一个 dired 缓冲区中?

例如,我刚刚在 dired 模式下打开了一个 Python 模块,创建了一个新目录,在 dired 中打开了该目录,现在需要在该目录中添加一个空的 __init__.py 文件。

如果我使用 Cx Cf __init__.py RET Cx Cs 那么 emacs 不会创建该文件,因为没有对其进行任何更改。我必须输入文件,保存它,删除我输入的内容,然后再次保存它才能工作。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

眸中客 2024-09-04 03:51:19

这是 dired-create-directory 的改编版。它的工作方式相同,因此除了普通文件名之外,您还可以为文件指定新的父目录(在当前目录下创建)(例如 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))))))

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))))))
澉约 2024-09-04 03:51:19

Emacs 不允许您保存缓冲区,除非它认为内容已更改。最快(尽管可能不是最干净)的方法是使用 Cx Cf 打开文件,然后按(例如)空格和退格键,然后您应该能够保存没有内容的文件。

还有其他方法可以更改“缓冲区已修改”标志,但我认为没有更简单的方法。

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.

不甘平庸 2024-09-04 03:51:19

使用触摸命令。

M-! touch __init__.py RET

Use touch command.

M-! touch __init__.py RET
谈情不如逗狗 2024-09-04 03:51:19

最短的方法

通过 shell 操作创建一个空文件(但不打开它):

M-! > __init__.py RET

打开新文件:

Cx Cf RET

(注意:我们不'不必再次输入名称,因为新文件自动成为首选)

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)

优雅的叶子 2024-09-04 03:51:19

如果您已经打开了空文件,(shell-command (concat "touch " (buffer-file-name))) 将执行您想要的操作。

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

明明#如月 2024-09-04 03:51:19

除了页面上的其他答案之外,您还可以使用 f.el 的函数f-touch

M-:(f-touch“__init__.py”)RET

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

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

放赐 2024-09-04 03:51:19

您可以通过运行 将空缓冲区标记为已修改设置缓冲区修改-p。然后当你保存它时,Emacs 将写入该文件。

M-;                         ; Eval
(set-buffer-modified-p t)   ; Mark modified
C-x C-s                     ; Save buffer

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
霓裳挽歌倾城醉 2024-09-04 03:51:19

我在 dired 中使用以下绑定到 t 的内容。

(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))

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))
箜明 2024-09-04 03:51:19

我修改了 MrBones 的答案,并使用键绑定创建了自定义函数:

; 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)

这对于不花时间在新的 Python 项目文件夹中的任何地方创建 init.py 的重复操作非常有用。

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.

你又不是我 2024-09-04 03:51:19

最好的选择是:

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

The best option would be:

(with-temp-file "filename"
  (insert ""))
那支青花 2024-09-04 03:51:18

您可以使用触摸命令:

M-! touch __init__.py RET

You can use the touch command:

M-! touch __init__.py RET
冷清清 2024-09-04 03:51:18

以下作品:

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

如果您位于 Dired 缓冲区中,该文件将保存在此处显示的目录中。

诀窍是首先通过切换到不存在的名称来创建一个空缓冲区。然后写出文件。

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.

情感失落者 2024-09-04 03:51:18

如果您希望 Emacs 将所有新文件视为已修改,您可以像这样自动化解决方案:

(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)))

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)))
放手` 2024-09-04 03:51:18

以编程方式且不依赖 touch,这非常简单:

(with-temp-buffer (write-file "path/to/empty/file/"))

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

(with-temp-buffer (write-file "path/to/empty/file/"))
韵柒 2024-09-04 03:51:18

在此线程之后,Emacs 添加了两个新命令:

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

这些命令将在 emacs 27.1 版本中可用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文