I often want to calculate something quickly then stick it into the buffer I am currently editing. Previously, I would do something like type (+ 3 5) into the buffer, Type C-x C-e, see the result in the minibuffer at the bottom of the screen (are re-evaluate repeatedly if I didn’t quite catch it, say for a long result), type the result into the buffer, then delete the expression (although sometimes in C code, I might leave the expression in a comment).
Eventually, I realized that Esc-: brought up an eval prompt in the minibuffer and that I could use it instead of the above. But I still had to be fast at reading the result before it was taken of the screen, and now I had to retype the expression to re-evaluate it. Sometimes this was an improvement, but not always.
Alternatively, sometimes I would type (insert (format “%s” (+ 3 5))), and evaluate it, but that was quite a lot of typing, and so I’d only use that when I wanted a long result to be pasted into my buffer. And then I still had to delete that long expression.
That led me to do the following in my .emacs files:
(defun eval-exp-insert (EVAL-EXPRESSION-ARG) "Evaluate EVAL-EXPRESSION-ARG and insert the result into the current buffer." (interactive "xEval: \n") (insert (format "%s" (eval EVAL-EXPRESSION-ARG)))) (global-set-key "\M-:" 'eval-exp-insert)
With that, when I type Esc-:, and then enter (+ 3 5), 8 is inserted into the buffer at the current cursor position.