Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
up vote 8 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.