Autocomplete is an essential software tool in modern software environment. In this post I describe of how I set it up for Clojure in Vim.

To setup autocomplete in Vim first install the following plugin:

I suggest using Pathogen as that implies only a git clone to add a plugin into vim bundle directory.

vim-firebase won’t work out-of-the-box because there are two additional steps that need to be done.
First make sure to add cider-nrepl plugin into your profile file at vim ~/.lein/profiles.clj

{:user
  {:plugins [[cider/cider-nrepl "0.25.5"]] }}

To find the latest version of cider-nrepl check clojars.org . First version tag I used did not work for me.

After editing profiles.clj running Leiningen REPL will download the plugins and also start a local REPL server which Vim can connect to.

lein repl
Retrieving cider/cider-nrepl/0.25.5/cider-nrepl-0.25.5.pom from clojars
Retrieving cider/cider-nrepl/0.25.5/cider-nrepl-0.25.5.jar from clojars
nREPL server started on port 35419 on host 127.0.0.1 - nrepl://127.0.0.1:14123
REPL-y 0.4.4, nREPL 0.7.0
Clojure 1.10.1
OpenJDK 64-Bit Server VM 11.0.9+11-post-Debian-1deb10u1
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

Note the nrepl://host:port (it’s different every time).

If you open Vim in another shell/window you can now connect to Repl using vim-firebase.
Type the following while in Vim editor on a Clojure source file to connect to REPL (substituting “port” for correct value).

:FireplaceConnect nrepl://127.0.0.1:<port>

This is a bit tedious to do everytime. To make it work seamlessly it’s possible to fixate host and port with configuration. Create ~/.nrepl/nrepl.edn configuration file for nREPL server

mkdir -p ~/.nrepl/nrepl.edn

Edit it to look like the following (this makes it run on same port everytime)

{:bind         "localhost"
 :port         11111}

Now server is listening on a fixed port so it’s not a guessing game.

:FireplaceConnect nrepl://127.0.0.1:11111

As a programmer I always aspire to automate more.
To simplify running nREPL server one easy way is to use the following Bash script.

var="$(ps aux | grep "lein repl :headless" | grep -v grep)"
if [ "$var" ]; then
    echo "Clojure REPL running"
else
    echo "Starting Clojure nREPL server"
    lein repl :headless &
fi

To manually start REPL run:

cd ~
./leinrepl.sh &

To connect to a running REPL using previous settings run

lein repl :connect 127.0.0.1:11111

I did not manage to make REPL connect on Vim startup completely automated but the following works.
In ~/.vimrc I added a custom command

command Firestarter FireplaceConnect nrepl://127.0.0.1:11111/

This command can then be run in Vim command mode:

:Firestarter
Scope connection to: ~/github/project # hit enter here

Enjoy the spoils! SPOILS

Note: This procedure was tested on Debian GNU/Linux 10 (buster) and stock Vim 8.1.

Edits:

  • 2020-12-11 - I discovered that on reboot REPL is unresponsive. I modified text to reflect that fact.