How do I create an empty file in emacs?

前端 未结 15 545
轻奢々
轻奢々 2020-12-13 03:20

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,

相关标签:
15条回答
  • 2020-12-13 04:00

    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.

    0 讨论(0)
  • 2020-12-13 04:00

    The best option would be:

    (with-temp-file "filename"
      (insert ""))
    
    0 讨论(0)
  • 2020-12-13 04:08

    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))))))
    
    0 讨论(0)
  • 2020-12-13 04:09

    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.

    0 讨论(0)
  • 2020-12-13 04:09

    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)

    0 讨论(0)
  • 2020-12-13 04:09

    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))
    
    0 讨论(0)
提交回复
热议问题