Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

1 Answer 1

up vote 6 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

 
discard

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.