If you use library Replace+ (replace+.el
) then you can just customize option search/replace-region-as-default-flag
to non-nil
. That gives you the region text as FROM
text default, so you can just hit RET
to accept that.
search/replace-region-as-default-flag
is a variable defined in replace+.el
.
Its value is nil
Documentation:
Non-nil
means use the active region text as default for search/replace.
That is, if the region is currently active then use its text as the
default input. All text properties are removed from the text.
Note that in this case the active region is not used to limit the
search/replacement scope. But in that case you can of course just
narrow the buffer temporarily to restrict the operation scope.
A non-nil
value of this option takes precedence over the use of option
search/replace-2nd-sel-as-default-flag
. To give that option
precedence over using the active region, you can set this option to
nil and use region-or-non-nil-symbol-name-nearest-point
as the value
of option search/replace-default-fn
.
You can customize this variable.
You can also use command toggle-search/replace-region-as-default
anytime to toggle the option value.
But if you really want not to even have to hit RET
to accept the region text then you'll need to write a command that uses that as the FROM
arg to query-replace
and then interactively reads the other args.
This should do that:
(defun my-q-r (from-string to-string
&optional delimited start end backward region-noncontiguous-p)
"Query-replace text of active region with text you're prompted for."
(interactive
(progn
(barf-if-buffer-read-only)
(let* ((from (if (use-region-p)
(buffer-substring (region-beginning) (region-end))
(query-replace-read-from "Query replace" nil)))
(to (if (consp from) (prog1 (cdr from) (setq from (car from)))
(query-replace-read-to from "Query replace" nil))))
(list from to
(or (and current-prefix-arg (not (eq current-prefix-arg '-)))
(and (plist-member (text-properties-at 0 from) 'isearch-regexp-function)
(get-text-property 0 'isearch-regexp-function from)))
(and current-prefix-arg (eq current-prefix-arg '-))))))
(deactivate-mark)
(query-replace from-string to-string delimited start end backward region-noncontiguous-p))