ProgramingTip

emacs에서 빈 파일을 어떻게 생성합니까?

bestdevel 2020. 11. 26. 19:41
반응형

emacs에서 빈 파일을 어떻게 생성합니까?


이상적으로 dired 버퍼 내에서 emacs에서 빈 파일을 어떻게 만들 수 있습니까?

예를 들어, 방금 dired 모드에서 Python 모듈을, 새 디렉토리를 만들고, dired에서, 이제 __init__.py디렉토리에 파일 을 추가해야합니다 .

내가 사용하면 C-x C-f __init__.py RET C-x C-s변경 사항이 없기 때문에 emacs가 파일을 생성하지 않습니다. 파일을 입력하고 저장하고 내 입력을 삭제 한 다음 다시 저장해야합니다.

감사합니다


다음은 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))))))

터치 명령을 사용할 수 있습니다.

M-! touch __init__.py RET

다음 작업 :

Cx b __init__.py RET Cx Cw RET

dired 버퍼에있는 경우 파일은 여기에 디렉토리에 저장됩니다.

트릭은 먼저 존재하지 않는 이름으로 전환하여 빈 버퍼를 만드는 것입니다. 그런 다음 파일을 작성하십시오.


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

Emacs는 내용이 변경 될 수 없습니다. 가장 빠르지 만 가장 파일을 사용하지 않는 파일을 연 C-x C-f다음 스페이스와 백 스페이스 키를 사용할 수 있습니다.

"버퍼가 수정"플래그를 변경하는 방법이 더 쉬운 방법은 생각합니다.


프로그래밍 방식으로 의존하지 않고 touch매우 독립적입니다.

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

터치 명령을 사용하십시오.

M-! touch __init__.py RET

Emacs는 두 가지 새로운 명령을 추가했습니다.

  1. 빈 파일 만들기
  2. dired-create-empty-file

이 명령은 emacs 27.1 릴리스에서 사용할 수 있습니다.


가장 짧은 방법

M-! > __init__.py RET


(shell-command (concat "touch " (buffer-file-name))) 이미 빈 파일을 연 경우 원하는 작업을 수행합니다.


페이지의 다른 답변 외에도 f.el의 기능을 사용할 수 있습니다 f-touch.

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


를 실행하여 빈 버퍼를 수정 된 것으로 표시 할 수 있습니다 set-buffer-modified-p. 그런 다음 저장하면 Emacs가 파일을 작성합니다.

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

나는 tdired에서 다음과 같은 바인딩을 사용합니다 .

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

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 를 생성하는 반복적 인 작업에 시간을 소비하지 않는 데 매우 유용 합니다.


가장 좋은 방법은 다음과 같습니다.

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

참고 URL : https://stackoverflow.com/questions/2592095/how-do-i-create-an-empty-file-in-emacs

반응형