Please tell me the command to delete "A certain character A" and "C" between "B" in Emacs with confirmation (query).
As a concrete example, we would like to "remove only one blank between two "x".What commands do you want to run this on?
Assume that the buffer contains:
x x x x y
The desired results are as follows:
xxxxy
For example, if you run M-%x x RET xx RET
, replacing all of them with !
will naturally fail to meet the requirements:
xxxxy
I can only think of doing a replacement twice or redefining the replace-search-function, but I'm asking because I think there's already a solution in a common situation.
emacs
According to the query-replace-regexp
documentation
Ininteractive calls, the replacement text can contain`\,'
followed by a Lisp expression.Each
replacement events that expression to compute the replacement
string.Inside of that expression, `\&' is a string denoting the
where match as a string, `\N' for a partial match, `\#&' and `\#N'
for the whole or a partial match converted to a number with
`string-to-number', and `\#'itself for the number of replacements
done so far (starting with zero).
I can write a Lisp expression, so
\(x\)+x
\, (replace-regexp-in-string""\&)
Or
\, (make-string(1+(/(length\&)2))?x)x)
You can replace it with .
The Emacs regular expression does not have lookahead, so M-x query-replace-regexp
seems impossible.
If necessary, I will write Elisp without thinking deeply:
(while(search-forward "xx")
(if (y-or-n-p "replace?)") (replace-match "xx")
(backward-char1); go back to the last x and search again
What's the rule if you don't want to replace the item in the string? has a way to call Perl and use lookahead, which means you can rewrite it all at once without a query.
(defun whatver-you-want()
"Putproper expansion here"
(interactive nil)
(while (search-forward "xx")
(if (y-or-n-p "replace?)")
(replace-match "xx")
(backward-char1))
And I've defined it as a function, and I've done it, but I think it's going to do the expected movement. (I wanted to comment on Camlspotter's post, but I still didn't have enough reputations, so I answered independently.)
© 2024 OneMinuteCode. All rights reserved.