0

If I want, for example, add all numbers in a line with a keyboard macro, how can I do it?

It would add: 1 1 34 53 into: 89.

Or, given a word: asdf transform it into fdsa

I know how to define the macro, bind it, and name it.

I have no interest in inputting the numbers in a table. This is meant for easy of use, like typing amount, interest, years, and getting a total amount.

1

It's probably best not to try doing this with a keyboard macro. However, a simple elisp function to do it is easy to write:

(defun db48x/add-up-numbers (beg end)
  (interactive "r")
  (let ((sum (apply #'+
                    (mapcar #'string-to-number
                            (split-string (buffer-substring beg end))))))
    (delete-region beg end)
    (insert (number-to-string sum))))

It operates on the region, so select the text containing the numbers first. You can bind it to the key of your choice or call it directly (M-x db48x/add-up-numbers).

| improve this answer | |
0

Have you tried C-x Q during macro definition? It's for putting in minibuffer prompts for getting variable arguments into the macro at runtime.

As for adding a row of numbers in a region, you may find M-x Quick-Calc more convenient.

The last option of course is to edit the macro and insert something like this:

(prompt (if arg (read-from-minibuffer "PROMPT: ") "Input: "))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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