首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在emacs中创建空文件?

如何在emacs中创建空文件?
EN

Stack Overflow用户
提问于 2010-04-07 19:49:56
回答 9查看 59.2K关注 0票数 68

如何从emacs创建一个空文件,最好是从dired buffer中创建?

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

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

谢谢

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2013-09-19 10:44:37

这是dired-create-directory的改编版本。它的工作方式与普通文件名相同,因此您还可以为文件(例如foo/bar/filename)指定新的父目录(将在当前目录下创建)。

代码语言:javascript
复制
(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))))))
票数 12
EN

Stack Overflow用户

发布于 2010-04-07 19:51:49

您可以使用touch命令:

代码语言:javascript
复制
M-! touch __init__.py RET
票数 60
EN

Stack Overflow用户

发布于 2010-04-07 20:57:35

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

代码语言:javascript
复制
(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)))
票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2592095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档