我想在dired模式下创建一个新文件。dired模式中是否有“创建新文件”命令?例如,当我在dired模式下键入“c”时,它会创建“untitled.txt”。这很简单,但是我找不到它。
只需按下C-x C-f。 这将提示输入文件名,使用当前缓冲区的当前目录作为放置文件的目录。 对于dired缓冲区,其当前目录就是您查看的目录。
touch filename
。这仅适用于非Windows系统,并且仅适用于您尚未在Emacs中打开shell的情况。 - Robin Green如果您希望在Dired模式下使用c
来执行C-x C-f
的操作,答案是微不足道的:
(define-key dired-mode-map "c" 'find-file)
如果你想要它的名称为untitled.txt
,那么:
(define-key dired-mode-map "c"
(lambda () (interactive) (find-file "untitled.txt")))
(with-eval-after-load 'dired ...)
将其包装起来。 - Jay感谢大家,我终于自己解决了问题。这里是我的答案。在dired模式下键入“c”将提示您创建新的无标题文件。然后按回车键将创建新的无标题文件。是的,代码非常冗长。有人可能会修复它。
(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))
)
)
)
完成以上步骤后,Emacs 将创建一个以你输入的名称命名的空缓冲区。但是 Emacs 默认不支持创建空文件。你可以参考如何在 Emacs 中创建空文件获取更多想法。
touch
在 $PATH
中 - 或者可以使用绝对路径。 touch
通常在Unix系统中可用,例如OSX等。此函数将自动按顺序编号文件/缓冲区,例如 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-n-p (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-n-p (format "Open `%s'? "lawlist-filename))
(error "Done."))
(find-file lawlist-filename)
(text-mode))
(t (message "You have exited the function.")) )) ))
untitled.txt
已经存在会发生什么?相信我,这个问题已经解决了,C-x C-f
是最简单的方法。 - abo-abotouch
的 analong,承认一下吧。 - Dodgie