Michał Kwiatkowski
Michał Kwiatkowski

Reputation: 9764

How to invoke an interactive elisp interpreter in Emacs?

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.

Upvotes: 41

Views: 12641

Answers (6)

Greg Mattes
Greg Mattes

Reputation: 33949

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"

Upvotes: 69

Ray
Ray

Reputation: 192216

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

Upvotes: 21

Alex Ott
Alex Ott

Reputation: 87144

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

Upvotes: 2

jfm3
jfm3

Reputation: 37774

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

Upvotes: 8

Kyle Burton
Kyle Burton

Reputation: 27528

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$

Upvotes: 1

cjm
cjm

Reputation: 62099

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

Upvotes: 1

Related Questions