代码之家  ›  专栏  ›  技术社区  ›  Singletoned

如何在Emacs中创建空文件?

  •  60
  • Singletoned  · 技术社区  · 15 年前

    如何从Emacs创建一个空文件,最好是从一个脏的缓冲区中创建?

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

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

    谢谢

    15 回复  |  直到 6 年前
        1
  •  10
  •   phils    11 年前

    这是对 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))))))
    
        2
  •  55
  •   matthias krull Taqveem    11 年前

    您可以使用触摸命令:

    M-! touch __init__.py RET
    
        3
  •  27
  •   slu    15 年前

    以下工作:

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

    如果您在一个脏的缓冲区中,文件将保存在此处显示的目录中。

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

        4
  •  20
  •   Scott Weldon Marc-Alexandre Bérubé    10 年前

    如果希望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)))
    
        5
  •  12
  •   Vatine    15 年前

    Emacs不允许保存缓冲区,除非它认为内容已更改。最快但可能不干净的方法是使用 C-X C-F型 ,然后按空格键和退格键,您应该可以保存一个没有内容的文件。

    还有其他方法可以更改“buffer has been modified”标志,但我不认为有任何更简单的方法。

        6
  •  10
  •   joao    10 年前

    可编程且不依赖于 touch 很容易:

    (with-temp-buffer (write-file "path/to/empty/file/"))
    
        7
  •  5
  •   Uchenna Nwanyanwu sarin    12 年前

    使用触摸命令。

    M-! touch __init__.py RET
    
        8
  •  4
  •   thanhpk    8 年前

    最短的路

    M! > __init__.py 雷特

        9
  •  4
  •   Tino    6 年前

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

    1. 生成空文件
    2. 目录创建空文件

    这些命令将在Emacs27.1版本中提供。

        10
  •  3
  •   Squidly    12 年前

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

        11
  •  2
  •   Mirzhan Irkegulov    11 年前

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

    M: (f-touch "__init__.py") 雷特

        12
  •  1
  •   Ian Mackinnon    11 年前

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

    M-;                         ; Eval
    (set-buffer-modified-p t)   ; Mark modified
    C-x C-s                     ; Save buffer
    
        13
  •  1
  •   Rorschach    7 年前

    我使用以下绑定到 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))
    
        14
  •  0
  •   alex_koval    11 年前

    我已经修改了mrbones的答案,并创建了带有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)
    

    这对于不花时间重复创建 初始化 .py在新的python项目文件夹中无处不在。

        15
  •  0
  •   Marcin Antczak    9 年前

    最好的选择是:

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