Windows 10, Emacs 25.1

I want to do some arithmetic operation I do this: enter image description here

The result is in the echo area, but I want the result to be in cursor place in the buffer. Something like this:

enter image description here

How do I do this?

  • Is that supposed to be a "won't"? If yes, Emacs may be the wrong editor for you...
    – wasamasa
    Commented Sep 9, 2017 at 12:16
  • Emacs is not wrong editor fo me. I use it many years. Sorry for my English. It's not my native language. Commented Sep 9, 2017 at 13:05
  • I have a function in my emacs config that I think I got from the bottom of this post. Commented Sep 9, 2017 at 17:21
  • If you are in the *scratch* buffer, C-j will evaluate the expression before the cursor and place the value of the expression in the buffer.
    – gboffi
    Commented Sep 9, 2017 at 22:47

5 Answers 5


Short version: yes

Instead of C-x C-e to evaluate the expression, give it a prefix argument. C-u C-x C-e will print the output to the buffer.

How I found this information

You can investigate how Emacs does these things by looking in the manual, or asking Emacs itself.

To see what a particular keybinding does, you can use C-h k (describe-key). You were evaluating the expression with C-x C-e, and you can figure out what that keybinding calls with C-h k C-x C-e. That will display the command's docstring, the first part of which is:

C-x C-e runs the command eval-last-sexp (found in global-map), which is an interactive compiled Lisp function in elisp-mode.el.

It is bound to C-x C-e.


Evaluate sexp before point; print value in the echo area. Interactively, with a non - prefix argument, print output into current buffer.


I highlighted the key phrase: giving it a prefix argument (C-u) will print the output to the buffer, rather than the echo area.

  • 2
    It's good, but it not enough. If I use command C-u C-x C-e the result will be (* 60 3)180. But I need only 180 (as in my screenshot). Commented Sep 9, 2017 at 13:03
  • 1
    @user8542613 : then you will need to write your own command.
    – Dan
    Commented Sep 9, 2017 at 13:12
  • 3
    @user8542613 : I think Drew's answer does exactly what you want. Please consider switching accepted answers.
    – Dan
    Commented Sep 9, 2017 at 15:12

Bind this to some key.

(defun foo ()
  "Replace sexp before point by result of its evaluation."
  (let ((result  (pp-to-string (eval (pp-last-sexp) lexical-binding))))
    (delete-region (save-excursion (backward-sexp) (point)) (point))
    (insert result)))

If you want to do an arithmetic operation and insert the value into the buffer, but don't care where you do the operation, then you can also do C-u M-: and type the operation info the minibuffer.

This blog has

(defun eval-and-replace (value)
  "Evaluate the sexp at point and replace it with its value"
  (interactive (list (eval-last-sexp nil)))
  (kill-sexp -1)
  (insert (format "%S" value)))

I prefer to do (also complex) computations in Emacs' calc C-x * * and then copy its result in the buffer where my cursor was with y, q closes the calc buffers and I'm back at this location.

  • 1
    This is what I use all the time. Also, if you want an algebraic expression, you can do C-x * q, enter an expression, then the answer will be on the kill ring, so you can insert it into the buffer with C-y. Commented Aug 21, 2021 at 10:05

I had the same wish and hadn't used Emacs in a long while and forgot how to do it :) Very simple and touched on by some above answers with the prefix command C-u

Simply C-u M-: (Eval buffer appears for your function) such as Eval: (- 5 2) and the result of 3 will appear in your buffer cursor location

This is very handy for kbd-macros where you need to manipulate some arithmetic and replace values

  • If you're in Lisp interaction buffer mode, like *scratch*, C-j with pos after last expression parenthesis will evaluate it. Note that such evaluation is postion dependent, so place point where the expression is complete.
    – Muihlinn
    Commented Jun 4, 2020 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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