How do I start the REPL in a user defined namespace?

10,234

Solution 1

java -cp .;clojure-1.3.0.jar; clojure.main -e \
"(ns dbx) (clojure.main/repl) (in-ns 'dbx) (clojure.core/use 'clojure.core)"

Solution 2

Nowadays is :repl-options {:init-ns foo.bar}.

See https://github.com/technomancy/leiningen/blob/master/sample.project.clj

Solution 3

If you are using Leiningen to build your project, then add this to your project's project.clj file:

(defproject test "1.0.0-SNAPSHOT"
  :description "FIXME: write description"
  :dependencies [[org.clojure/clojure "1.2.1"]]
  :main test.core)

In your src/test/core.clj file, add this to create a test.core namespace:

(ns test.core)

(defn -main [& args])

Next, build your project with Leiningen with lein compile. Then enter lein repl to invoke the REPL in your namespace. The REPL prompt will look like:

test.core=>

Solution 4

In addition to Carlos' answer suggesting :repl-options {:init-ns foo.bar}, I have also had success with adding :dev {:main user} to my profile.clj.

To give more context:

;; /foo/profile.clj
...
:main foo.core
:dev {:main user
      :source-paths ["dev"]}`
...

;; /foo/dev/user.clj
(ns user
  (:require
   [clojure.pprint :refer (pprint)]
   [clojure.repl :refer :all]
   [clojure.string :as str]
   [clojure.test :refer [run-tests run-all-tests]]
   [clojure.tools.namespace.repl :refer [refresh refresh-all]]))

Solution 5

Using tools.deps one way would be to define an alias and execute some forms in it:

:aliases
      {:cursive {:main-opts ["-e" "(load \"de/sveri/getless/user\")"
                             "-e" "(de.sveri.getless.user/reset)"]}

This will load the de.sveri.getless.user namespace and execute the de.sveri.getless.user.reset function afterwards.

Share:
10,234

Related videos on Youtube

vikbehal
Author by

vikbehal

Currently working on Sharepoint 2013, Ruby, Office365, Azure, asp, c#.

Updated on June 04, 2022

Comments

  • vikbehal
    vikbehal over 1 year

    Writing (in-ns 'dbx) to a file and loading it isn't changing the default namespace of the repl (using cygwin/console). The namespace is still user=>, not dbx=>.

    vikrant[28] clj
    Clojure 1.3.0
    user=> (load-file "try1.clj")
    #(Namespace dbx)
    user=>
    

    How can we start the REPL in a namespace defined in a script file?

    • vikbehal
      vikbehal almost 12 years
      (ns dbx) (clojure.main/repl) (in-ns 'dbx) (clojure.core/use 'clojure.core)
    • Alex Jasmin
      Alex Jasmin almost 12 years
      Just a quick comment on formating. It's should be cleaner and easier to simply copy/paste the text in your cygwin console than taking a screen-shot of it.
  • David J.
    David J. almost 9 years
    I recommend using Carlos's more up-to-date answer.
  • Carlos Fontes
    Carlos Fontes over 6 years
    @LincolnBergeson it still works for me using latest leiningen - version 2.7.1, with a brand new app project created with lein new app.