如何在emacs中创建一个空文件? [英] How do I create an empty file in emacs?

查看:258
本文介绍了如何在emacs中创建一个空文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从emacs创建一个空文件,理想情况下是从一个可靠的缓冲区中?

How can I create an empty file from emacs, ideally from within a dired buffer?

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

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.

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

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.

谢谢

推荐答案

以下是 dired-create-directory 的修改。它的工作方式相同,以及一个纯文件名,您还可以为文件指定新的父目录(在当前目录下创建)(例如 foo / bar / 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))))))

这篇关于如何在emacs中创建一个空文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆