简体   繁体   中英

How to auto say Yes when run a command in Emacs?

I had to run the command revert-buffer many times recently and really frustrated to say yes whenever emacs prompts this message Revert buffer from file abc.txt? (yes or no) Revert buffer from file abc.txt? (yes or no) .

Is there anyway to auto say yes in this case?

If it's just for interactive usage, I'd define an alternative function:

(defun my-revert-buffer-noconfirm ()
  "Call `revert-buffer' with the NOCONFIRM argument set."
  (interactive)
  (revert-buffer nil t))

Alternatively, as the revert-buffer docstring tells you, take a look at the revert-without-query variable, in case that's a nicer solution for you.

在相关的旁注中,许多人在他们的.emacs中有以下行,只需按一个键即可确认(仅yn ):

(defalias 'yes-or-no-p 'y-or-n-p)

I use this, similar to what @phils proposed , but with non- nil IGNORE-AUTO arg:

(defun revert-buffer-no-confirm ()
  "Revert buffer without confirmation."
  (interactive) (revert-buffer t t))

And I bind it to <f5> , since that's what that key does generally, in MS Windows.

In any case, I agree (strongly) with those who have advised to define a separate command for this. I would not bother with revert-without-query , unless you are very sure wrt certain files ( always ) etc. It's best to let revert-buffer continue to act normally, and provide (and perhaps bind) your own command for interactive use. You know best when to not be bothered by a confirmation prompt.

自定义revert-without-query可能是一种选择。

While it is, as pointed out by other answers, preferred to redefine the revert-buffer function or the key binding, it is possible to auto-reply "yes" to any function using yes-or-no-p or for that matter y-or-np by something like the following:

(defalias 'yes-or-no-p '(lambda (a &rest b) t))

This may be very destructive to your data, so use at your own discretion.

I use the following to turn all 'yes/no' confirmations to 'y/n' confirmations, and default to yes for certain prompts. i add prompts to default-yes-sometimes as needed. note i don't have to list the entire prompt, just a regexp that matches it.

(setq original-y-or-n-p 'y-or-n-p)
(defalias 'original-y-or-n-p (symbol-function 'y-or-n-p))
(defun default-yes-sometimes (prompt)
  (if (or
       (string-match "has a running process" prompt)
       (string-match "does not exist; create" prompt)
       (string-match "modified; kill anyway" prompt)
       (string-match "Delete buffer using" prompt)
       (string-match "Kill buffer of" prompt)
       (string-match "Kill Dired buffer of" prompt)
       (string-match "delete buffer using" prompt))
      t
    (original-y-or-n-p prompt)))
(defalias 'yes-or-no-p 'default-yes-sometimes)
(defalias 'y-or-n-p 'default-yes-sometimes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM