YesOrNoP

‘y-or-n-p’ is one way that Emacs asks a yes-or-no question, usually to get your confirmation for something you requested.

‘yes-or-no-p’ is another way that Emacs uses to do this. It is typically used for more important yes-or-no questions, that is, questions where the answer might have more serious consequences. You must type a full ‘yes’ or ‘no’ to answer ‘yes-or-no-p’. See the Elisp manual doc for yes-or-no-p.

The author of a function prompts users for a yes-or-no answer decides which of these prompting functions to use. For functions delivered as part of vanilla Emacs, the designers of Emacs have presumably based their decisions on years of experience with user problems. You are asked to type a full word not to annoy you but to save you from shooting yourself in the foot.

Some Users Want to Always Use `y-or-n-p', Never `yes-or-no-p'

However, Emacs design decisions need to be relatively conservative, and they need to fit the needs of the average user, as well as the EmacsNewbie.

Some users apparently feel that no ‘yes-or-no-p’ prompts are important enough to warrant typing two or three characters, and they prefer to substitute ‘y-or-n-p’ for ‘yes-or-no-p’ everywhere, to save typing one or two characters. If you want to do that, put this in your init file (~/.emacs):

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

Is that a good idea? Depends on the user, presumably.

Auto-Confirmation: Automatically Saying Yes

Some ‘yes-or-no-p’ prompts can be disabled. For example, RevertBuffer asks before rereading a file, but you can set user option ‘revert-without-query’ to record files that you want to be able to revert without confirming.

And you can advise a function (see AdvisingFunctions) to prevent it from asking yes or no and just assume yes. But if you do this, be careful that it doesn’t also catch lower-level questions. And see also AdviceVsHooks before you decide to advise a function.

    (defadvice SOME-FUNCTION (around auto-confirm compile activate)
      (flet ((yes-or-no-p (&rest args) t)
             (y-or-n-p (&rest args) t))
        ad-do-it))

Without advising, here’s another way to force a function to act (assume yes) without asking for confirmation: use (no-confirm SOME-FUNCTION).

  (cl-flet ((always-yes (&rest _) t))
    (defun no-confirm (fun &rest args)
      "Apply FUN to ARGS, skipping user confirmations."
      (cl-letf (((symbol-function 'y-or-n-p) #'always-yes)
                ((symbol-function 'yes-or-no-p) #'always-yes))
         (apply fun args))))

Responding To `yes-or-no-p' Prompts Without Typing

You can predefine the ‘yes’ and ‘no’ answers for ‘yes-or-no-p’ so you can select them (retrieving them as past inputs) using ‘M-p’.

    (mapcar (lambda (elt) (add-to-list 'yes-or-no-p-history elt))
            '("no" "yes"))

If you would prefer to have an empty response (hitting ‘RET’) or any other input to act as ‘no’, but still require the full word ‘yes’ to confirm, you can use this function as an alias for ‘yes-or-no-p’.

  (defun my-yes-or-mumble-p (prompt)
    "PROMPT user with a yes-or-no question, but only test for yes."
    (if (string= "yes"
                 (downcase
                  (read-from-minibuffer
                   (concat prompt "(yes or no) "))))
        t
      nil))
  
    (defalias 'yes-or-no-p 'my-yes-or-mumble-p)

Library `yes-no.el': Let Users Choose

For Lisp code that you write, you make a good judgment about whether to use ‘yes-or-no-p’ or ‘y-or-n-p’ for each confirmation prompt context. Still, users can have different preferences in this regard.

You can give users a way to override your choices on a calling function-by-function basis, by using library yes-no.el. All you need to do, to give them the choice, is to add the calling function as an extra argument when you call either ‘yes-or-no-p’ or ‘y-or-n-p’ in your code.

E.g., instead of writing this confirmation prompt, in the definition of function ‘foo’:

    (yes-or-no-p "Agree? ")

You write this one:

    (yes-or-no-p "Agree? " 'foo)

Or if your judgment tells you that ‘y-or-n-p’ is probably more appropriate, you write this one:

    (y-or-n-p "Agree? " 'foo)

Either way, you give users the choice. You provide, in effect, a default prompting behavior for caller ‘foo’, but users can override your prompting choice.

Library yes-no.el redefines functions ‘yes-or-no-p’ and ‘y-or-n-p’ to provide for the extra CALLER argument (e.g. ‘foo’).

Users can respond to the prompt using, as before, ‘yes’ or ‘no’ – or ‘y’ or ‘n’. But they can also interrupt answering to tell Emacs to change to the other prompting style from now on, whenever prompting is done by the given caller. (If they’ve already done that then they can also tell Emacs to revert to the default, programmer-defined prompting.)

How does it work? When a CALLER (e.g. ‘foo’) arg is provided to one of the prompting functions:

When ‘yes-or-no-p’ prompts and either ‘use-y-or-n’ or ‘reset-to-default’ is available as a response, completion is available for these inputs. (In addition, if option ‘yn-allow-completion-for-yes/no-flag’ is non-nil then ‘yes-or-no-p’ provides completion for ‘yes’ and ‘no’ also.)

Use choices are saved persistently, in option ‘yn-prompting-alist’. Users can of course customize the option directly, to define calling function-specific prompting behavior.

Other Possibilities


CategoryKeys CategoryCode