3

I wrote a bad bit of elisp when trying to do a query-replace-regexp.

The text to replace was like this:

* list item

And I wanted it to be

* List item

My query-replace-regexp match was written as:

^\* \([a-z]\{1\}\) 

And the match, where I used the wrong elisp, was:

* \,(upcase-word \1)  ;; should have been * \,(capitalize \1)

I get this error: Wrong type argument: number-or-marker-p, replace-eval-replacement

If I try to run query-replace-regexp again, I get the same error and cannot edit the replacement text. All I can do is exit Emacs and restart it. I assume there must be some way to clear the history, but I can't find it.

Suggested on twitter was (setq regexp-history nil) but no luck.

This is Emacs 25.0.50.

4
  • Not sure I follow what you're trying to do, but if you run query-replace-regexp again, you can scroll through your history (eg, with the arrow keys) to select and edit the regexp and the to-string.
    – Dan
    Commented Dec 17, 2014 at 0:33
  • That's the exact problem - I can't. If I try to run it again, I get that same error and I can't see any of the previous entries to edit. That's why I'm trying to clear out the history - any ideas on another way to get rid of the error? You captured exactly what I want to do, but Emacs won't let me, so that's why I assumed I need to clear out that entry somehow. Commented Dec 17, 2014 at 0:37
  • Odd -- I entered the exact same search parameters and got the exact same error. When I M-x query-replace-regexp again, it lets me scroll through the history with no problem. Do you get the same behavior with no init file (emacs -Q)?
    – Dan
    Commented Dec 17, 2014 at 0:47
  • Started emacs w/o the init file but I still see it. Now this is really weird. It changes what happens depending on how many times I run it. Run once, I can scroll it back. Run again, get that error and I'm stuck just like originally. Would seem to suggest I've got something weird in my init file maybe. Commented Dec 17, 2014 at 0:56

2 Answers 2

5

If you only want to clear the default values that come up when restarting query-replace-regexp, you can clobber them with:

(setq query-replace-defaults nil)

If you want to nuke the entire history as well, you can do:

(setq query-replace-history nil)
1
  • Second option did it - thanks so much. First option let me get back to the history and scroll, and the error message was one of the history entries. So no idea what's happening there. Commented Dec 17, 2014 at 0:59
6

@Dan's answer is good, and it clears the bad entry out of your history for good.

Just FYI, another possibility would have been to do C-x ESC ESC, and then edit the search pattern.

That brings up the full last command you executed, as a Lisp sexp, which you can edit. (If you had used other commands in the meantime then you could use M-p to move backward in this history (the extended-command history), which is different (and acts differently) from the query-replace-history.)

That would have let you use query-replace-regexp again without clearing its history. Not that there is any reason here not to clear the history, but it can be good to know of more than one way to skin the cat.

2
  • That's why I love this site: I never even knew about C-x ESC ESC.
    – Dan
    Commented Dec 17, 2014 at 1:54
  • @Dan: It's a great command. It also provides yet another (good) way to learn Emacs Lisp, as it shows you the code that you, in effect, just executed by invoking a command, whatever that command might be.
    – Drew
    Commented Dec 17, 2014 at 1:55

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.