emacs write-file 如何自动 overwrite,去掉回复yes,no


自己在使用 write-file 函数写一些功能时,老是出现提示是否 overwrite已经存在的文件,如何默认直接 overwrite,忽略掉这个提示。


先删除后保存 :rofl:





(defun write-file (filename &optional confirm)
  "Write current buffer into file FILENAME.
This makes the buffer visit that file, and marks it as not modified.

If you specify just a directory name as FILENAME, that means to use
the default file name but in that directory.  You can also yank
the default file name into the minibuffer to edit it, using \\<minibuffer-local-map>\\[next-history-element].

If the buffer is not already visiting a file, the default file name
for the output file is the buffer name.

If optional second arg CONFIRM is non-nil, this function
asks for confirmation before overwriting an existing file.
Interactively, confirmation is required unless you supply a prefix argument."
;;  (interactive "FWrite file: ")
   (list (if buffer-file-name
	     (read-file-name "Write file: "
			     nil nil nil nil)
	   (read-file-name "Write file: " default-directory
			    (file-name-nondirectory (buffer-name))
			   nil nil))
	 (not current-prefix-arg)))
  (or (null filename) (string-equal filename "")
	;; If arg is a directory name,
	;; use the default file name, but in that directory.
	(if (directory-name-p filename)
	    (setq filename (concat filename
				    (or buffer-file-name (buffer-name))))))
	(and confirm
	     (file-exists-p filename)
	     ;; NS does its own confirm dialog.
	     (not (and (eq (framep-on-display) 'ns)
		       (listp last-nonmenu-event)
	     (or (y-or-n-p (format-message
                            "File `%s' exists; overwrite? " filename))
		 (user-error "Canceled")))
	(set-visited-file-name filename (not confirm))))
  (set-buffer-modified-p t)
  ;; Make buffer writable if file is writable.
  (and buffer-file-name
       (file-writable-p buffer-file-name)
       (setq buffer-read-only nil))
  ;; It's likely that the VC status at the new location is different from
  ;; the one at the old location.



write-file 是个相当高层的用户命令,不清楚你为什么要从 Lisp 调用它,如果可能,应优先考虑 write-region



如果你只是觉得每次打“yes"和“no"很烦的话,可以使用 (fset 'yes-or-no-p 'y-or-n-p) 改为只用"y"和“n“来给出选择。就按一个键,怎么都不算麻烦吧