I am using following solution (How do I add a keybinding to use during an interactive isearch, `C-s ...`) to move over symbols in the buffer, which iterates over the matching symbols:
(define-key isearch-mode-map (kbd "C-w")
'isearch-forward-symbol-at-point)
Is it possible to apply same manner into replace-all where instead of text it replaces the symbols (where the isearch-forward-symbol-at-point iterates over):
My query replace function, which replace all the matching query instead of only the symbols:
(defun query-replace-region-or-from-top ()
(interactive)
(let ((case-fold-search nil))
(progn
(let ((orig-point (point)))
(if (use-region-p)
(call-interactively 'query-replace)
(save-excursion
(goto-char (point-min))
(call-interactively 'query-replace)))
(message "Back to old point.")
(goto-char orig-point)))))
(global-set-key "\C-x\C-r" 'query-replace-region-or-from-top)
Example_1:
gas_costs = 100
_cost = 10
log(f"==> cost={_cost}")
Replace-all _cost into cost
=> changes into:
gascosts = 100
cost = 10
log(f"==> cost={cost}")
wanted:
gas_cost = 100 # should remain unchanged
cost = 100
log(f"==> cost={cost}")
Example_2:
alper = 100
alper99 = 99
Replace-all alper into sad
=> changes into:
sad = 100
sad99 = 99
wanted:
sad = 100
alper99 = 99
I just want to replace patterns as exactly same with isearch finds. Because first I always search patterns using isearch and than replace them. But if there are diffierent matched patterns in replace-all, I get end up replacing differect results than isearch finds.
replace-allis not correct way to put it. I just wanted to search and replace in the entire buffer starting from first appearance in the buffer till end. When I tried your solution it replaces the_costintgas_costwhich I don't want – alper May 02 '22 at 01:49_costingas_costshould not get replaced, and it works like expected here. I am sure that the pattern is correct. Please try again, maybe usingemacs -Q– dalanicolai May 02 '22 at 08:18emacs -Qchangingas_costintogascost:-( – alper May 02 '22 at 12:36\([[:space:]{]\|^\)+_cost(copy-paste it) after doingM-x query-replace-region-or-from-top(the version I've posted here). Otherwise, I am not sure what kind of Emacs you are using ;) – dalanicolai May 02 '22 at 13:18\([[:space:]{]\|^\)+_costembedded into the function and act as its default behavior? Like if I give_cost, the function convert it into the pattern – alper May 10 '22 at 14:04query-replace-region-or-from-topgenerates:The mark is not set now, so there is no region. Is it possible to select the region as complete buffer by default and ask what to replace with? – alper Jul 04 '22 at 22:16_cost=>costwas just an example, it can be_cost=>emacs123or anything. I just wanted apply replace operation to replace matches same as the find words/patterns inisearch-forward-symbol-at-point– alper Jul 04 '22 at 22:16isearch-forward-symbol-at-pointwhen you search for_costit does not found_costin thegas_costsince it search for the words – alper Jul 05 '22 at 09:51\b,\<and\_<. Then practice a little/do some tests usingreplace-regexporregexp-builder. – dalanicolai Jul 07 '22 at 06:17isearchfinds. Because first I always search patterns inisearchand than replace them but hence all diffierent matched patterns also changed in replace-all – alper Jul 07 '22 at 08:14