Calculation of Inbuffer emacs

Is it possible to do inbuffer calculation in emacs.

For example, if my file has the following numbers

10
eleven
12

(A) I would like to convert these numbers to hex (either in place or paste it next to it),

10 A
11 B
12 C

(B) I would like to summarize these numbers.

10
eleven
12

33

(C) I would like to increment the counter (sth as an index)

10
eleven
12
thirteen
14

+4
source share
1 answer

You can use the built-in calculator and / or the fact that \, in a replacement string for commands like replace-regexp will evaluate an arbitrary elisp expression.

You can do more or less than the top of your head:

a.

  • Mark the area containing the numbers.
  • Run Mx replace-regexp
    • For the corresponding regular expression, use \([[:digit:]]+\) .
    • To replace, use \,(format "%X" (string-to-number \1)) .

IN.

  • Mark the area containing the numbers.
  • Enter Cx * g .
  • Enter VR + .
  • Enter y to insert the amount, or Cu y to replace.

FROM.

Same as for A , but mark only the last number and use the replace function \,(format "%s\n%d" \1 (1+ (string-to-number \1))) .

You can put them in macros or functions that take care of moving the point to the right place.

+9
source

All Articles