The title isn't entirely accurate. By "non-interactively" I
mean, for lack of a better term, "semi-interactively". In a regular
call through M-%
or M-x query-replace
, the command could be called interactive
in two ways: 1) by giving the string to replace and the replacement 2)
by choosing one by one which matches to replace. The idea is to skip
the former step by giving fixed arguments (string-to-replace and
replacement-string), but to keep the latter interactive query
behavior.
Often a queried search & replace task needs to be repeated over time
(i.e. search and replace the same strings) so the obvious next step is
to write a simple command, for example, to create a
fix-two-or-more-spaces
command replacing " \\{2,\\}"
with a
single space. How to achieve this? Must admit I got a little
intimidated by perform-replace
...
perform-replace
already contains the answer: usere-search-forward
plusreplace-match
perform-replace
its usage is simple in simple use-cases. Example:(perform-replace "\\([[:space:]]\\)\\{2,\\}" "\\1" t t nil)
.query-replace
query-replace
and "non-interactively".query-replace
is inherently interactive, hence my comment