A Nice Emacs Trick

In the comments to my Another VimGolf in Emacs Challenge post, Aaron Culich points out something that I didn’t know. It is possible to enter query-replace or query-replace-regexp directly from isearch. When you do this, query-replace and query-replace-regexp will use the search string for the FROM string and only ask you for the TO string. You invoke the query-replace functions from isearch by typing 【Meta+%】 or 【Ctrl+Meta+%】 before quitting isearch.

Why would you want to do this? Here’s an example. Suppose you have code like

(defun anothercase ()
  ...)
...
(anothercase)
...
(anothercase)
...
etc

and that your cursor is at the defun. You decide you want to change the name of the anothercase function to make-new-instance. If you type 【Ctrl+s a Ctrl+w】 the search string will be anothercase and you can type 【Meta+%】 to start query replace with the FROM string already set.

A final note. You don’t have to do anything to enable this functionality. In particular, it is not controlled by the query-replace-interactive variable. That, if non-nil, always uses the last isearch string as the FROM string for the query-replace functions, which is probably not what you want.

This entry was posted in General and tagged . Bookmark the permalink.