20

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?

Drew
  • 77,472
  • 10
  • 114
  • 243
user8542613
  • 663
  • 6
  • 16

5 Answers5

24

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.

(eval-last-sexp EVAL-LAST-SEXP-ARG-INTERNAL)

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.

Dan
  • 32,980
  • 7
  • 102
  • 169
  • 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). – user8542613 Sep 09 '17 at 13:03
  • 1
    @user8542613 : then you will need to write your own command. – Dan Sep 09 '17 at 13:12
  • 3
    @user8542613 : I think Drew's answer does exactly what you want. Please consider switching accepted answers. – Dan Sep 09 '17 at 15:12
9

Bind this to some key.

(defun foo ()
  "Replace sexp before point by result of its evaluation."
  (interactive)
  (let ((result  (pp-to-string (eval (pp-last-sexp) lexical-binding))))
    (delete-region (save-excursion (backward-sexp) (point)) (point))
    (insert result)))
Drew
  • 77,472
  • 10
  • 114
  • 243
5

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)))
icarus
  • 1,914
  • 1
  • 10
  • 16
2

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.

Dieter.Wilhelm
  • 1,906
  • 14
  • 25
  • 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. – Daniel Doherty Aug 21 '21 at 10:05
0

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

Rick G
  • 1
  • 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 Jun 04 '20 at 08:38