2011-03-26

IELM: a REPL for emacs

Emacs-lisp (elisp) is a nice language to play around with code and try things as you develop them – explorative programming. I often use the *scratch* buffer for that, but sometimes it's nice to use a so-called 'REPL' ( Read-Eval-Print-Loop) instead. A REPL is a sort-of command-line interface where your expressions are evaluated as soon as they are considered 'complete' and you press Enter.

So, enter Emacs's built-in repl: IELM. You can activate it with M-x ielm, and the interaction looks something like the following:

*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> 123 
123
ELISP> (+ 1 2)
3
ELISP> ;; comment
ELISP> (defun fac (n)
         (if (= 0 n)
           1
           (* n (fac (- n 1)))))
fac
ELISP> (fac 5)
120
ELISP> 

By default, IELM evaluates complete expressions automatically as soon you as you press Enter. So one thing to remember is that if you want to have multi-line expression (like above), you must make sure that after each line the expression is not complete (i.e., the brackets are not balanced) -- otherwise the expression will be evaluated too early. That makes modes like autopair or paredit a bit inconvenient for this.

If you don't like that behavior, you can do:

(setq ielm-dynamic-return nil)

which will allow you to Enter as much as you want and only evaluate things when you press C-j. But then you might as well use *scratch* I suppose. Personally, I use IELM mostly as a calculator.

7 comments:

Anonymous said...

If you're using IELM as a calculator, you really need to read up on Emacs calc! It's 100x better for that purpose.

djcb said...

@Anonymous: well, I know calc, but I've never been totally happy with the interaction model, even when using algebraic model. It's also a bit of overkill. Anyway, maybe I should try to give it another chance.

Michael Hoffman said...

I was just coming here to suggest calc myself.

Anonymous said...

Try out C-x * q

It's kinda like M-x calculator.

Ruben Berenguel said...

I didn't know emacs lisp had a repl. Cool! As for doing calculations... When I need to calculate something, I fire Pari/GP in a terminal and then discard it when I'm finished. Usually Pari is overkill, but sometimes I just leave it open and start coding a little (Pari is used for high precision computation and number theoretic stuff).

Cheers,

Ruben

Joseph G Gay said...

IELM is also immensely useful when developing and testing minor and major modes, due to the following feature: "The current working buffer may be changed (with a call to `set-buffer',
or with C-c C-b), and its value is preserved between successive
evaluations. In this way, expressions may be evaluated in a different
buffer than the *ielm* buffer. By default, its name is shown on the
mode line; you can always display it with C-c C-v, or the buffer itself
with C-c C-f."

Walrus said...

isnt't eshell also an elisp repl, and much more? i use eshell all the time for this -- or for shorter things, eval-expression (M-:).