Every time when I call M-x projectile-regenate-tags I get bothered with the message

Tags file a:/foobar/TAGS has changed, read new contents? (y or n)

which is quite cumbersome. Especially when I wanted to add the command to a after-save-hook.

So I was thinking about a defadvice, or command to press y in the minbuffer that I could do in a function same with (projectile-regenate-tags).

Any idea in order to do so?

  • 1
    Find the code that invokes the prompt, and act accordingly. Maybe there is a hook or an option that affects the behavior. Or maybe you need to advise or even redefine some function. The devil is in the details. IOW, look at the code.
    – Drew
    Commented Aug 11, 2015 at 20:28

3 Answers 3


The tags-revert-without-query variable is the generic mechanism for skipping this prompt.

Check whether setting that directly circumvents the problem. If so, you could let-bind it in a wrapper around the function you're calling to get that behaviour for this scenario only.

  • This is a more clever way to do. Thanks. If I might ask, how did you know that such variable exists?
    – ReneFroger
    Commented Aug 12, 2015 at 12:00
  • 2
    You can (and should) ask Emacs. M-x apropos-user-option RET tag RET is a useful approach for discovering tag-related config options (n.b. use after the library has loaded, or Emacs won't know about all the options). Or you might use the broader apropos-variable. There are many useful apropos-* commands (which, naturally, you can discover with M-x apropos-command RET apropos RET).
    – phils
    Commented Aug 12, 2015 at 12:47
  • I get it, very useful indeed. Thanks.
    – ReneFroger
    Commented Aug 12, 2015 at 20:27

Here's a way to do that generically for any function (even if it has no convenient variable)

(defun y-or-n-p-test ()
  (message (if (y-or-n-p "is this true?")

(defun auto-yes (old-fun &rest args)
  (cl-letf (((symbol-function 'y-or-n-p) (lambda (prompt) t))
             ((symbol-function 'yes-or-no-p) (lambda (prompt) t)))
    (apply old-fun args)))

(defun auto-no (old-fun &rest args)
  (cl-letf (((symbol-function 'y-or-n-p) (lambda (prompt) nil))
             ((symbol-function 'yes-or-no-p) (lambda (prompt) nil)))
    (apply old-fun args)))

(advice-add #'y-or-n-p-test :around #'auto-yes)

;; just testing
  • Thanks Pythonnut for your answer. Technically it's the answer that I was looking for, but I decided to go for phils, because his answer is only targeting the question, while your question applies for all sort of questions, which is not desirable. Nevertheless, your input is really appreciated!
    – ReneFroger
    Commented Aug 12, 2015 at 12:01
  • I'd rather define an auto-answer advice which would answer automatically whatever a global dynamical variable tells it to answer. Then auto-answering would amount to (let ((that-global-var value)) code-asks-questions). WDYT ?
    – YoungFrog
    Commented Aug 16, 2016 at 10:37
  • @YoungFrog that would be as simple as replacing the nil or t inside the lambda with your variable.
    – PythonNut
    Commented Aug 16, 2016 at 15:05
  • @PythonNut Almost, yes. I wrote the code and posted it as an answer (too long to fit here).
    – YoungFrog
    Commented Aug 18, 2016 at 9:30

This is only a variation on PythonNut's answer but it's too long to fit properly in a comment. The main difference is that I advise once, and then the desired behaviour is obtained by binding a dynamical variable.

;; -*- mode: emacs-lisp; lexical-binding: t; -*-

(defvar auto-answer 'ask
  "When bound, yes-or-no questions will not prompt.

When this variable is bound to a boolean value, the functions
`yes-or-no-p' and `y-or-n-p' will return that value without
prompting.  With its default value (the symbol `ask'), the
functions will prompt normally.")

(defun auto-answer (oldfun &rest args)
  (if (booleanp auto-answer)
    (apply oldfun args)))

(advice-add 'y-or-n-p :around 'auto-answer)
(advice-add 'yes-or-no-p :around 'auto-answer)

;; test it:
(y-or-n-p "foo")
(let ((auto-answer t))
  (y-or-n-p "foo"))
(let ((auto-answer nil))
  (y-or-n-p "foo"))
  • Thanks for sharing. I never expected I would get an useful answer on this exactly 1 year later! Thanks!
    – ReneFroger
    Commented Aug 18, 2016 at 12:22
  • An idea for improvement would be to make the behaviour dependent on the message in the prompt.
    – YoungFrog
    Commented Aug 19, 2016 at 18:47
  • I wrote a version which acts depending on the prompt text, it's available at github.com/YoungFrog/auto-answer (slightly different calling convention, look at the example at the top)
    – YoungFrog
    Commented Sep 1, 2016 at 12:20
  • Thanks, looks interesting. Do you mind if it was posted on reddit.com/r/emacs?
    – ReneFroger
    Commented Sep 1, 2016 at 20:31
  • @ReneFroger I don't mind
    – YoungFrog
    Commented Sep 1, 2016 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.