If I want to do some math. operation I do next:

(* 5 3)

C-x C-e (eval-last-sexp)

Here result:

enter image description here

As you can see the result is in the minibuffer.


But I need result to REPLACE math. formula. I need smt. like this:

enter image description here How I can do this?

P.S. Custom function eval-last-sexp not work for multiple cursors

enter image description here

enter image description here

enter image description here

2 Answers 2


First, in case you weren't aware, there's eval-print-last-sexp, bound to C-j in lisp-interaction-mode (for instance in the *scratch* buffer), which will print the result after the sexp (instead of displaying it in the echo area, like eval-last-sexp, but without deleting the sexp, like you want).

The following function should also delete the sexp, giving the desired effect of replacing the sexp with its desired value.

(defun eval-replace-last-sexp ()
  "Evaluate sexp before point; replace sexp with value.

The point will be at the end of the inserted value.  The replaced
sexp will be added to the kill-ring.  If the latter is not
desired, use `delete-region' instead of `kill-region', below."
    (let ((beg (progn
      (end (progn
      ;; the argument of t means that result is printed into the buffer,
      ;; as we want
      (eval-last-sexp t)
      (kill-region beg end)))

Now you can call the function directly with M-x eval-replace-last-sexp or by binding it to a convenient keyboard short-cut.

  • It's not work for multiple cursors. It work only when I has ONE cursor. I update my post. Jan 21, 2019 at 12:01
  • Does the built-in eval-print-last-sexp (C-j in *scratch*) work with multiple cursors? (I haven't tested that yet, as I don't use multiple-cursors; I'll try to get around to it.)
    – aplaice
    Jan 21, 2019 at 12:23
  • Yes, eval-print-last-sexp successfully works for multiple cursors ONLY by C-j. If I run by name eval-print-last-sexp - than I need run every time this function. Jan 21, 2019 at 12:40
  • If I bind keybinding to eval-print-last-sexp, e.g. C-c C-l and run eval-print-last-sexp by shortcut it works with multiple-cursors. Jan 21, 2019 at 12:46
  • 1
    Investigating a bit, I think that it's just the behaviour of multiple cursors. For instance, if you use M-x kill-line directly (rather than with C-k), then it also won't work with multiple cursors. I believe that the behaviour is intentional and caused by execute-extended-command (the function that M-x is bound to) being in mc--default-cmds-to-run-once (see here). The custom eval-replace-last-sexp also seems to work with multiple cursors when bound to a key.
    – aplaice
    Jan 21, 2019 at 15:20

Embedded calc mode might of interest to you. Not for elisp maths, however. Check the emacs calc tutorial (info manual).

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.