How to search/replace string for the whole buffer in Emacs without going back to the beginning?

Solution 1:

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.

Solution 2:

(defun my-replace-string ()
  (interactive)
  (save-excursion
    (beginning-of-buffer)
    (call-interactively 'replace-string)))

Solution 3:

I use iedit for this. Very useful.