6

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 Sep 9 '17 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. – user8542613 Sep 9 '17 at 13:05
  • I have a function in my emacs config that I think I got from the bottom of this post. – ShreevatsaR Sep 9 '17 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 Sep 9 '17 at 22:47
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.

(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.

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

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)))
3

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)))
1

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.

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.