5

When I do bulk calculations in a scratch buffer, it is annoying to have to delete the actual expressions after they are evaluated (I want them to be deleted on evaluation).

Example:

(+ 2 2)

Pressing C-u C-x C-e gives:

(+ 2 2)4

What I want instead is only this (the expression has been removed):

4

Is there a command that I'm not aware of? I'm curious if there is an easy work around before I write something of my own.

2 Answers 2

6

Two possibilities that I am aware of.

  1. In the package crux https://github.com/bbatsov/crux there is a function crux-eval-and-replace which does exactly what you are asking for. If you don't want to use the whole package you can just take this function.
(defun crux-eval-and-replace ()
  "Replace the preceding sexp with its value."
  (interactive)
  (let ((value (eval (elisp--preceding-sexp))))
    (backward-kill-sexp)
    (insert (format "%S" value))))
  1. There is also the package lispy, see https://github.com/abo-abo/lispy that I like very much for editing lisp files. It has a function lispy-eval-and-replace which also does what you want but might be an overkill if you don't want to use the package.
1
  • it's funny, I pretty much collected my own crux over time. It could have saved me a whole lot of time.
    – A_P
    Feb 19, 2019 at 14:33
2

Try writing the expression in the minibuffer instead. Using eval-expression with the universal prefix argument inserts the result:

C-u M-: (+ 2 2) RET
1
  • Not related to the question. I do bulk calculation on thousands of lines in scratch buffer. Typing it out is not an option by any means.
    – A_P
    Feb 20, 2019 at 14:16

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.