It is really inconvenient to first go back to the buffer top and then do the search/query. Well, for search, it is relatively simple that you can just press C-s one more time to wrap around, but for query/replace, it's tedious.

Any simple hack to do that without going back to the buffer top for both operations?


Huh, looks like you can't (taken from here, emphasis mine):

To replace every instance of 'foo' after point with 'bar', use the command M-x replace-string with the two arguments foo and bar. Replacement occurs only after point, so if you want to cover the whole buffer you must go to the beginning first.

Personally, I split the buffer in two (C-x 2), go to the top (C-Home) and then run the replace command, switch back to my original pane (C-x o) and then kill the second (C-x 0). Don't know if there is a trick to make that simpler.

(defun my-replace-string ()
    (call-interactively 'replace-string)))
  • Works nicely, but screws up query-replace on region/selection (always does on the whole buffer). – Alex Jan 8 '15 at 8:34

I use iedit for this. Very useful.


I've using below for work with Emacs 24+:

;; query replace all from buffer start
(fset 'my-query-replace-all 'query-replace)
(advice-add 'my-query-replace-all
            #'(lambda(oldfun &rest args)
               "Query replace the whole buffer."
               ;; set start pos
               (unless (nth 3 args)
                 (setf (nth 3 args)
                       (if (region-active-p)
               (unless (nth 4 args)
                 (setf (nth 4 args)
                       (if (region-active-p)
               (apply oldfun args)))
(global-set-key "\C-cr" 'my-query-replace-all)

Regard the region replace case, and any START and END args passed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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