Clojure standard REPL with buffer editor?
Asked Answered
T

2

6

I am not looking for an IDE or integrated REPL. I just wanted to ask if someone knows if it is possible to have a buffer editor in the REPL as know from 'psql' or some *nix shells.

In psql for example you just enter "\e" and you'll get the last command in your EDITOR and uppon exit it gets executed.

This would be awesome for clojure repl.

$ java -cp jline.jar:clojure-1.3.0.jar jline.ConsoleRunner clojure.main

So, is there such a feature? Where would it have to be implemented, in jline?

Solution:

rlwrap does the trick.

I had to compile readline ftp://ftp.gnu.org/gnu/readline/ and then rlwrap http://utopia.knoware.nl/~hlub/rlwrap/#rlwrap.

After that I could use the following shortcut to start the editor.

Ctrl + ^

rlwrap -m -- java -cp clojure-1.3.0.jar clojure.main

For the use with leiningen on Mac OSX I had to change the following:

lein 1.6.2
*** 226,233 ****
              rlwrap -m -q '"' echo "hi" > /dev/null 2>&1
              if [ $? -eq 0 ]; then
                  RLWRAP="$RLWRAP -r -m -q '\"'"
-             else
-                 RLWRAP="$RLWRAP -m --"
              fi
          fi
      fi
--- 226,231 ----

and export RLWRAP_EDITOR

export RLWRAP_EDITOR="vim +%L"

Typographer answered 11/1, 2012 at 22:41 Comment(0)
T
8

rlwrap

There are a number of ways to achieve what you want, but the rlwrap binary does what you need. I've also had some versioning issues with jline in the past on Mac OS X, so stopped using it.

When I type 'clj' in my terminal, it really executes the REPL wrapped with rlwrap via the following bash script in /usr/local/bin/clj:

#!/bin/sh
# Runs clojure.
# With no arguments, runs Clojure's REPL.

CLOJURE=$CLASSPATH:/usr/local/clojure/1.3.0/clojure-1.3.0.jar

rlwrap java -cp $CLOJURE clojure.main "$@"

Put the script anywhere in your path, to use.

Finally, you don't need the script, I just have it as a convenience. If rlwrap is on your system and clj is in your path, then you can simply type:

rlwrap clj

Further reading from the Clojure wiki book: Enhancing Clojure REPL with rlwrap

Tobiastobie answered 11/1, 2012 at 23:22 Comment(0)
H
6

See my answers to the questions One REPL to bind them all? and How to install Clojure on Ubuntu 10.04 from Github repo with no clojure.jar. Main points:

  • Don't "install" clojure at all. Install Leiningen, and let it manage the versions of clojure you have, as well as getting the classpaths right for repl or other usage.
  • If you must launch clojure by hand, use rlwrap or some other readline program.
Horsy answered 11/1, 2012 at 23:36 Comment(1)
Thanks for the tip, getting the feature with leiningen would have been my second step. Thought it would be simpler without. But as far as I can see, leiningen uses rlwrap automatically if present.Typographer

© 2022 - 2024 — McMap. All rights reserved.