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.

3 Answers 3

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
0

Eval and Replace

The solution here is:

  • No need to install any package.
  • Can be tested directly (see below)
  • Extension is possible with Macros (not mentioned here).

I found the best solution mentioned in the article here Eval and Replace

(defun eval-and-replace ()
  "Replace the preceding sexp with its value."
  (interactive)
  (backward-kill-sexp)
  (condition-case nil
      (prin1 (eval (read (current-kill 0)))
             (current-buffer))
    (error (message "Invalid expression")
           (insert (current-kill 0)))))

How to test

  1. [Optional] Create a buffer like playground.el and put the code mentioned above (In case you want to keep this snippet for the next session).
  2. Then highlight the code above, then M-x: eval-region
  3. You will get the eval-and-replace loaded to your buffer.
  4. Then simply test it on a lisp expression
  • Open another file called test.el
  • put lisp expression such as (+ 3 4)
  • Put the cursor at the end of this express
  • Run M-x: eval-and-replace function that we loaded. demo
3
  • Why do you say "no need to install any package"? Your recipe requires 3rd-party library playground.el, doesn't it? Maybe your first bullet should be removed?
    – Drew
    May 24, 2023 at 14:13
  • @Drew-san, I apologize for the unclarity of my previous post, I have adjusted. Indeed, you don't need any package, I mentioned these files (playground. el, test. el) for testing purposes only, I hope it is clear by now. Please let me know
    – Dr Neo
    May 24, 2023 at 14:30
  • Yep, clear now; thanks.
    – Drew
    May 24, 2023 at 15:16

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.