You don't need to define a separate function (command) for this. And even if you did define one, it need not use query-replace-regexp
.
The standard command query-replace
does just what you request, if you provide it with a prefix argument.
So just use C-u M-%
to query-replace words.
C-h f query-replace
tells you this (note the part in bold):
query-replace is an interactive compiled Lisp function in
`replace.el'.
It is bound to M-%
, <menu-bar> <edit> <replace> <query-replace>
.
(query-replace FROM-STRING TO-STRING &optional DELIMITED START END
BACKWARD)
Replace some occurrences of FROM-STRING with TO-STRING.
As each match is found, the user must type a character saying
what to do with it. For directions, type C-h at that time.
In Transient Mark mode, if the mark is active, operate on the contents
of the region. Otherwise, operate from point to the end of the buffer.
Use M-n to pull the last incremental search string to the minibuffer
that reads FROM-STRING, or invoke replacements from
incremental search with a key sequence like C-s C-s M-%
to use its current search string as the string to replace.
Matching is independent of case if case-fold-search
is non-nil and
FROM-STRING has no uppercase letters. Replacement transfers the case
pattern of the old text to the new text, if case-replace
and
case-fold-search
are non-nil and FROM-STRING has no uppercase
letters. (Transferring the case pattern means that if the old text
matched is all caps, or capitalized, then its replacement is upcased
or capitalized.)
Ignore read-only matches if query-replace-skip-read-only
is non-nil,
ignore hidden matches if search-invisible
is nil, and ignore more
matches using isearch-filter-predicate
.
If replace-lax-whitespace
is non-nil, a space or spaces in the string
to be replaced will match a sequence of whitespace chars defined by the
regexp in search-whitespace-regexp
.
Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
only matches surrounded by word boundaries. A negative prefix arg means
replace backward.
Fourth and fifth arg START and END specify the region to operate on.
To customize possible responses, change the bindings in query-replace-map
.
And if you insist that you want a separate command for this, then just do the obvious:
(defun q-r-word ()
"Query-replace whole words."
(interactive)
(let ((current-prefix-arg t))
(call-interactively #'query-replace)))
M-x replace-string RET
gnu.org/software/emacs/manual/html_node/emacs/Replace.html It starts from wherever point is and works its way to the end of the buffer. If you want the whole buffer, then go to the top and call the function. Or, there are a few threads that wrap thereplace-string
in asave-excursion
(or the equivalent thereof) that goes to the top and then returns point to where it was originally when the function ends. See also --M-x query-replace
-- gnu.org/software/emacs/manual/html_node/emacs/…multiple-cursors
library that can select all occurrences in the buffer; or one + the next one going forward and so on; or one + the next one going backward and so on. github.com/magnars/multiple-cursors.el Multiple occurrences can be edited simultaneously.