Question

Right now I write expressions in the *scratch* buffer and test them by evaluating with C-x C-e. I would really appreciate having an interactive interpreter like SLIME or irb, in which I could test Emacs Lisp expressions.

Was it helpful?

Solution

It's easy to evaluate Lisp expressions in Inferior Emacs-Lisp Mode:

M-x ielm

You can read more about this feature in the Emacs manual section on "Lisp Interaction"

OTHER TIPS

Eshell is another option for an interactive Elisp interpreter.

M-x eshell

Not only is it a command shell like bash (or cmd.exe if on Windows) but you can also interactively write and execute Elisp code.

~ $ ls
foo.txt
bar.txt
~ $ (+ 1 1)
2

Your best bet is the *scratch* buffer. You can make it more like a REPL by first turning on the debugger:

M-x set-variable debug-on-error t

Then use C-j instead of C-x C-e, which will insert the result of evaluating the expression into the buffer on the line after the expression. Instead of things like command history, * * * and so forth, you just move around the *scratch* buffer and edit.

If you want things like * * * to work, more like a usual REPL, try ielm.

M-x ielm

In the *scratch* buffer, just type C-j to evaluate the expression before point.

Well, if you're really interested in a literal REPL for emacs it is possible to write one using the -batch mode of emacs:

(require 'cl)

(defun read-expression ()
  (condition-case
      err
      (read-string "> ")
    (error
     (message "Error reading '%s'" form)
     (message (format "%s" err)))))

(defun read-expression-from-string (str)
  (condition-case
      err
      (read-from-string str)
    (error
     (message "Error parsing '%s'" str)
     (message (format "%s" err))
     nil)))

(defun repl ()
  (loop for expr = (read-string "> ") then (read-expression)
        do
        (let ((form (car (read-expression-from-string expr))))
          (condition-case
              err
              (message " => %s" (eval form))
            (error
             (message "Error evaluating '%s'" form)
             (message (format "%s" err)))))))

(repl)

You can call this from the command line, or, as you seem to want, from within an emacs buffer running a shell:

kburton@hypothesis:~/projects/elisp$ emacs -batch -l test.el
Loading 00debian-vars...
> (defvar x '(lambda (y) (* y 100)))
 => x
> (funcall x 0.25)
 => 25.0
> 
kburton@hypothesis:~/projects/elisp$

To run just one elisp expression you can use M-: shortcut and enter expression in mini-buffer. For other cases you can use scratch buffer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top