如何从dired模式创build新文件?
我想在dired模式下创build一个新的文件。 在直接模式下是否有“创build新文件”命令? 例如,当我在直接模式下键入“c”时,它会创build“untitled.txt”。 这很简单,但我找不到。
只要按Cx Cf. 这将提示input一个文件名,使用当前缓冲区的当前目录作为目录放置它。对于一个dired缓冲区,它的当前目录就是你正在查看的目录。
如果你想在Dired模式下用Cx Cf
做Cx Cf
,答案是微不足道的 :
(define-key dired-mode-map "c" 'find-file)
或者,如果你想要它的名字untitled.txt
然后:
(define-key dired-mode-map "c" (lambda () (interactive) (find-file "untitled.txt")))
感谢所有, 我终于自己解决了 。 这是我的答案。 在dired模式下键入“c”会提示您创build新的无标题文件。 然后按回车将创build新的无标题文件。 是的,这是非常详细的代码。 有人可能会修复它。
(eval-after-load 'dired '(progn (define-key dired-mode-map (kbd "c") 'my-dired-create-file) (defun create-new-file (file-list) (defun exsitp-untitled-x (file-list cnt) (while (and (car file-list) (not (string= (car file-list) (concat "untitled" (number-to-string cnt) ".txt")))) (setq file-list (cdr file-list))) (car file-list)) (defun exsitp-untitled (file-list) (while (and (car file-list) (not (string= (car file-list) "untitled.txt"))) (setq file-list (cdr file-list))) (car file-list)) (if (not (exsitp-untitled file-list)) "untitled.txt" (let ((cnt 2)) (while (exsitp-untitled-x file-list cnt) (setq cnt (1+ cnt))) (concat "untitled" (number-to-string cnt) ".txt") ) ) ) (defun my-dired-create-file (file) (interactive (list (read-file-name "Create file: " (concat (dired-current-directory) (create-new-file (directory-files (dired-current-directory)))))) ) (write-region "" nil (expand-file-name file) t) (dired-add-file file) (revert-buffer) (dired-goto-file (expand-file-name file)) ) ) )
以下包含两个(2)选项,其中一个选项要求在$PATH
中touch
– 或者,可以使用绝对path。 touch
通常在unix风味系统上可用,例如OSX等。这个function会自动为文件/缓冲区编号 – 例如untitled.txt; untitled1.txt; untitled2.txt等
(define-key dired-mode-map (kbd "c") 'dired-new-file) (defun dired-new-file () (interactive) (let* ( (n 0) lawlist-filename (dired-buffer-name (buffer-name))) (catch 'done (while t (setq lawlist-filename (concat "untitled" (if (= n 0) "" (int-to-string n)) ".txt")) (setq n (1+ n)) (if (not (file-exists-p lawlist-filename)) (throw 'done nil)) )) (message "[b]uffer + file (maybe) | [f]ile + buffer (maybe)") (let ((file-or-buffer (read-char-exclusive))) (cond ((eq file-or-buffer ?b) (switch-to-buffer (get-buffer-create lawlist-filename)) (text-mode) (or (y-or-np (format "Save Buffer `%s'? "lawlist-filename)) (error "Done.")) (write-file lawlist-filename) (with-current-buffer dired-buffer-name (revert-buffer))) ((eq file-or-buffer ?f) (start-process "touch-file" nil "touch" lawlist-filename) (revert-buffer) (or (y-or-np (format "Open `%s'? "lawlist-filename)) (error "Done.")) (find-file lawlist-filename) (text-mode)) (t (message "You have exited the function.")) )) ))
- 按Cx Cf
- input文件名称
- 按Cj
在完成上述步骤之后,emacs将会用你input的名字创build一个空的缓冲区。 但是emacs默认不支持创build空文件。 你可以参考我如何在emacs中创build一个空文件以获得更多的想法
如果你想input文件名,那么我认为这是重复的:
如何在emacs中创build一个空文件?
如果你不想input文件名,你仍然可以使用其中的一些答案,并通过硬编码名称而不是交互式地提示来适应其他人。