You could use this function for replacing strings
(defun replace-in-buffer ()
"Replace text in whole buffer. Change OLD string to NEW string"
(replace-string (read-string "OLD string:")
(read-string "NEW string:")
and bind it to key with
(global-set-key (kbd "C-c r") 'replace-in-buffer)
Thanks @rasmus, you could use:
M-x query-replace <from> RET <to> RET, then press
! for replacing all occurences and
C-u C-SPC twice to return point into the initial position.
query-replace bound to
M-%, this could be helpfil when replace regexps too How to search and replace in the entire buffer?