Michael
Michael

Reputation: 1963

Emacs query-replace-regexp reverse order of default

Sometimes I need to change a variable name in the code from foo to bar. I do it using query-replace-regexp. Then I would do some other stuff and I might regret my decision and want to change it back from bar to foo. If I do query-replace-regexp, it will show by default the last replacement performed (default foo -> bar). My question is, is there a quick way to tell Emacs I want the reverse order of the default? Otherwise one would need to type the whole thing again.

Edit: The default replacement is the last replacement (default foo -> bar). What I want now is the opposite: bar to foo. Basically, I want to undo the replacement. Not always can I use the undo feature since I may have a very long history after many other edits.

Upvotes: 3

Views: 441

Answers (2)

Drew
Drew

Reputation: 30699

When you use query-replace, M-p retrieves the default, which is foo -> bar. That is, it puts foo -> bar in the minibuffer.

You can edit that text in the minibuffer. In particular, you can move backward a word (or a sexp), using M-b (or C-M-b), and then use M-t (or C-M-t) to transpose the two words (sexps) there, giving you bar -> foo. (Then hit RET.)

The point is that once you have retrieved a previously used input, or the default input, you can edit it in the minibuffer.

(Normally, in Emacs, it is M-n that retrieves the default value. In this case it is M-p (which (IMHO) is an aberration).)


Wrt accessing previously entered input: You can cycle, by repeating M-p, but you can also search directly, using M-r. If the input you want to retrieve was not very recent, then M-r can be quicker than cycling.

Upvotes: 3

Randy Morris
Randy Morris

Reputation: 40927

This is probably a horrible way of doing this, but you could create a function like the following and bind it to another key. It seems to work in this specific case but there are tons of other things that this doesn't handle as is (such as the prefix argument).

(defun so-undo-last-query-replace ()
  (interactive)
  (let ((query-replace-defaults (list (cons (cdar query-replace-defaults)
                                            (caar query-replace-defaults)))))
    (call-interactively 'query-replace)))

Upvotes: 1

Related Questions