Is it possible to do the following in emacs:

Query replace all numbers in a region by the number +n, for example if you have:

12 and 7 are nice numbers

let n be 3, then it should transform to

15 and 10 are nice numbers

1 Answer 1


Yes, it's possible, \& can be used in replace expression to represent the entire match, similarly \#& can be used to represent the entire match as number.

More concretely:

M-x query-replace-regexp \b[0-9]+\b RETURN \,(+ 3 \#&)

And a quote from the documentation

You can use Lisp expressions to calculate parts of the replacement string. To do this, write ‘\,’ followed by the expression in the replacement string. Each replacement calculates the value of the expression and converts it to text without quoting.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .