nrepl.el
is an Emacs client for
nREPL, the Clojure networked
REPL server. It's a great alternative to the now deprecated combination
of SLIME + swank-clojure.
package.el
is the built-in package manager in Emacs 24+. On Emacs 23
you will need to get package.el yourself if you wish to use it.
nrepl.el
is available on both major package.el
community
maintained repos -
Marmalade and
MELPA.
If you're not already using Marmalade, add this to your
~/.emacs.d/init.el
(or equivalent) and load it with M-x eval-buffer.
(require 'package)
(add-to-list 'package-archives
'("marmalade" . "http://marmalade-repo.org/packages/"))
(package-initialize)
For MELPA the code you need to add is:
(require 'package)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)
And then you can install nREPL with the following command:
M-x package-install [RET] nrepl [RET]
or by adding this bit of Emacs Lisp code to your Emacs initialization file(.emacs
or init.el
):
(unless (package-installed-p 'nrepl)
(package-install 'nrepl))
If the installation doesn't work try refreshing the package list:
M-x package-refresh-contents [RET]
el-get is another popular package manager for Emacs. If you're an el-get user just do M-x el-get-install.
You can install nrepl.el
manually by placing nrepl.el
on your load-path
and require
ing it. Many people favour the folder ~/.emacs.d/vendor
:
(add-to-list 'load-path "~/emacs.d/vendor")
(require 'nrepl)
Keep in mind that nrepl.el
depends on clojure-mode
so you'll have to install
clojure-mode
as well.
nrepl.el
comes bundled in
Emacs Prelude. If you're a
Prelude user you can start using it right away.
nrepl.el
comes bundled in
Emacs Live. If you're using
Emacs Live you're already good to go.
You can certainly use nrepl.el
without configuring it any further,
but here are some ways other folks are adjusting their nrepl.el
experience.
- Enable eldoc in clojure buffers:
(add-hook 'nrepl-interaction-mode-hook
'nrepl-turn-on-eldoc-mode)
- You can hide the
*nrepl-connection*
and*nrepl-server*
buffers from appearing in some buffer switching commands likeswitch-to-buffer
(C-x b) like this:
(setq nrepl-hide-special-buffers t)
When using switch-to-buffer
, pressing SPC after the command will
make the hidden buffers visible. They'll always be visible in
list-buffers
(C-x C-b).
- You can control the TAB key behavior in the REPL via the
nrepl-tab-command
variable. While the default commandnrepl-indent-and-complete-symbol
should be an adequate choice for most users, it's very easy to switch to another command if you wish to. For instance if you'd like TAB to only indent (maybe because you're used to completing with M-TAB) use the following snippet:
(setq nrepl-tab-command 'indent-for-tab-command)
- Stop the error buffer from popping up while working in buffers other than the REPL:
(setq nrepl-popup-stacktraces nil)
- Enable error buffer popping also in the REPL:
(setq nrepl-popup-stacktraces-in-repl t)
- The nrepl buffer name takes the format
*nrepl project-name*
. Change the separator from space to something else by overridingnrepl-buffer-name-separator
.
(setq nrepl-buffer-name-separator "-")
- The nrepl buffer name can also display the port on which the nrepl server is running. Buffer name will look like nrepl project-name:port.
(setq nrepl-buffer-name-show-port t)
- Make C-c C-z switch to the
*nrepl*
buffer in the current window:
(add-to-list 'same-window-buffer-names "*nrepl*")
- Enabling
CamelCase
support for editing commands(likeforward-word
,backward-word
, etc) in nREPL is quite useful since we often have to deal with Java class and method names. The built-in Emacs minor modesubword-mode
provides such functionality:
(add-hook 'nrepl-mode-hook 'subword-mode)
- The use of paredit
when editing Clojure (or any other Lisp) code is highly
recommended. You're probably using it already in your
clojure-mode
buffers (if you're not you probably should). You might also want to enableparedit
in the nREPL buffer as well:
(add-hook 'nrepl-mode-hook 'paredit-mode)
- smartparens is an excellent
alternative to paredit. Many Clojure hackers have adopted it
recently and you might want to give it a try as well. To enable
smartparens
in the nREPL buffer use the following code:
(add-hook 'nrepl-mode-hook 'smartparens-mode)
- RainbowDelimiters is a minor mode which highlights parentheses, brackets, and braces according to their depth. Each successive level is highlighted in a different color. This makes it easy to spot matching delimiters, orient yourself in the code, and tell which statements are at a given depth. Assuming you've already installed RainbowDelimiters you can enable it in nREPL like this:
(add-hook 'nrepl-mode-hook 'rainbow-delimiters-mode)
- ac-nrepl provides completion source for the popular Emacs interactive auto-completion framework auto-complete. Where nrepl provides it, pop-up documentation for completed symbols will be displayed.
The only requirement to use nrepl.el is to have a nrepl server to
which it may connect. Many Clojurians favour the use of the Leiningen tool
to start a nrepl server, but the use of Leiningen is not a prerequisite to use
nrepl.el (but it's required if you want to use the nrepl-jack-in
command).
Leiningen is the de facto standard build/project management tool for Clojure. It has a similar scope to the Maven build tool favoured by Java developers (Leiningen actually reuses many things from the Maven ecosystem).
nrepl.el features a command called nrepl-jack-in
that will start an nrepl server
for a particular Leiningen project and connect to it automatically.
This functionality depends on Leiningen 2. Older versions are not supported. Follow
the installation instructions on Leiningen's web site to get it up and running and afterwards
create a project like this:
$ lein new demo
The two main ways to obtain an nREPL are discussed in the following sections of the manual.
Simply open in Emacs a file belonging to your lein
project (like
foo.clj
) and type M-x nrepl-jack-in. This will start a nREPL with
all the deps loaded in, plus an nrepl.el
client connected to it.
Alternative you can use C-u M-x nrepl-jack-in to specify the name of a lein project, without having to visit any file in it.
You can go to your project's dir in a terminal and type there (assuming you're using Leiningen that is):
$ lein repl
Alternatively you can start nrepl.el either manually or by the facilities provided by your project build tool (Maven, etc).
After you get your nrepl server running go back to Emacs. Typing there M-x nrepl will allow you to connect to the running nrepl session.
nrepl.el
comes with a handy minor mode called nrepl-interaction-mode
(complementing
clojure-mode
) that allows you to evaluate code in your Clojure source
files and load it directly in the repl. A list of all
available commands is available in the nREPL menu and in the following
section of this manual.
Make the REPL always pretty-print the results of your commands. Note
that this will not work correctly with forms such as (def a 1) (def b2)
and it expects clojure.pprint
to have been required already
(the default in more recent versions of Clojure):
M-x nrepl-toggle-pretty-printing
- M-x nrepl-jack-in: Launch an nrepl server and a repl client. Prompts for a project root if given a prefix argument.
- M-x nrepl: Connect to an already-running nrepl server.
While you're in clojure-mode
, nrepl-jack-in
is bound for
convenience to C-c M-j and nrepl
is bound to C-c
M-c.
Keyboard shortcut | Description |
---|---|
C-x C-e | Evaluate the form preceding point and display the result in the echo area. If invoked with a prefix argument, insert the result into the current buffer. |
C-c C-p | Evaluate the form preceding point and display the result in a popup buffer. |
C-M-x C-c C-c | Evaluate the top level form under point and display the result in the echo area. If invoked with a prefix argument, insert the result into the current buffer. |
C-c C-r | Evaluate the region and display the result in the echo area. |
C-c C-b | Interrupt any pending evaluations. |
C-c C-m | Invoke macroexpand-1 on the form at point and display the result in a macroexpansion buffer. If invoked with a prefix argument, macroexpand is used instead of macroexpand-1 . |
C-c M-m | Invoke clojure.walk/macroexpand-all on the form at point and display the result in a macroexpansion buffer. |
C-c C-n | Eval the ns form. |
C-c M-n | Switch the namespace of the repl buffer to the namespace of the current buffer. |
C-c C-z | Select the REPL buffer. With a prefix argument - changes the namespace of the REPL buffer to the one of the currently visited source file. |
C-u C-u C-c C-z | Select the REPL buffer based on a user prompt for a directory. |
C-c M-d | Display current REPL connection details, including project directory name, buffer namespace, host and port. |
C-c M-r | Rotate and display the current REPL connection. |
C-c M-o | Clear the entire REPL buffer, leaving only a prompt. Useful if you're running the REPL buffer in a side by side buffer. |
C-c C-k | Load the current buffer. |
C-c C-l | Load a file. |
C-c C-d | Display doc string for the symbol at point. If invoked with a prefix argument, or no symbol is found at point, prompt for a symbol. |
C-c C-s | Display the source for the symbol at point. If invoked with a prefix argument, or no symbol is found at point, prompt for a symbol. |
C-c C-j | Display JavaDoc (in your default browser) for the symbol at point. If invoked with a prefix argument, or no symbol is found at point, prompt for a symbol. |
M-. | Jump to the definition of a symbol. If invoked with a prefix argument, or no symbol is found at point, prompt for a symbol. |
M-, | Return to your pre-jump location. |
M-TAB | Complete the symbol at point. (For auto-complete integration, see ac-nrepl ) |
Keyboard shortcut | Description |
---|---|
RET | Evaluate the current input in Clojure if it is complete. If incomplete, open a new line and indent. If invoked with a prefix argument is given then the input is evaluated without checking for completeness. |
C-RET | Close any unmatched parenthesis and then evaluate the current input in Clojure. |
C-j | Open a new line and indent. |
C-c M-o | Clear the entire REPL buffer, leaving only a prompt. |
C-c C-o | Remove the output of the previous evaluation from the REPL buffer. |
C-c C-u | Kill all text from the prompt to the current point. |
C-c C-b C-c C-c | Interrupt any pending evaluations. |
C-up C-down | Goto to previous/next input in history. |
M-p M-n | Search the previous/next item in history using the current input as search pattern. If M-p/M-n is typed two times in a row, the second invocation uses the same search pattern (even if the current input has changed). |
M-s M-r | Search forward/reverse through command history with regex. |
C-c C-n C-c C-p | Move between the current and previous prompts in the REPL buffer. Pressing RET on a line with old input copies that line to the newest prompt. |
TAB | Complete symbol at point. |
C-c C-d | Display doc string for the symbol at point. If invoked with a prefix argument, or no symbol is found at point, prompt for a symbol |
C-c C-j | Display JavaDoc (in your default browser) for the symbol at point. If invoked with a prefix argument, or no symbol is found at point, prompt for a symbol. |
C-c C-z | Select the last clojure buffer. C-u C-c C-z will switch the clojure buffer to the namespace in the current buffer. |
C-u C-u C-c C-z | Select the clojure buffer based on a user prompt for a directory. |
C-c M-d | Display current REPL connection details, including project directory name, buffer namespace, host and port. |
C-c M-r | Rotate and display the current REPL connection. |
Keyboard shortcut | Description |
---|---|
C-c C-m | Invoke macroexpand-1 on the form at point and replace the original form with its expansion. If invoked with a prefix argument, macroexpand is used instead of macroexpand-1 . |
C-c M-m | Invoke clojure.walk/macroexpand-all on the form at point and replace the original form with its expansion. |
g | The prior macroexpansion is performed again and the current contents of the macroexpansion buffer are replaced with the new expansion. |
C-/ C-x u | Undo the last inplace expansion performed in the macroexpansion buffer. |
You can connect to multiple nREPL servers and use M-x nrepl-jack-in multiple times. To close a single nREPL session, use M-x nrepl-close. M-x nrepl-quit closes all sessions.
nrepl.el commands in a clojure buffer use the default connection. To make a connection default, switch to it's repl buffer and use M-x nrepl-make-repl-connection-default.
To switch to the relevant nREPL buffer based on the clojure namespace in the current buffer, use: C-c C-z.
You can display the current nREPL connection using C-c M-d and rotate through available connections using C-c M-r.
- Leiningen 2.x (only for
nrepl-jack-in
) - clojure-mode
- GNU Emacs 23.2+ or 24.
An extensive changelog is available here.
There are a couple of nrepl.el extensions that add some extra functionality to it:
- nrepl-tracing adds basic tracing support
- nrepl-decompile adds some Java bytecode decompilation commands
- Mailing list: https://groups.google.com/forum/#!forum/nrepl-el
- Please report issues on the GitHub issue tracker or the mailing list.
Bug reports and suggestions for improvements are always welcome. GitHub pull requests are even better! :-)
Install cask if you haven't already, then:
$ cd /path/to/nrepl
$ cask
Run all tests with:
$ make test
Copyright © 2012-2013 Tim King, Phil Hagelberg, Bozhidar Batsov, Hugo Duncan, Steve Purcell and contributors.
Distributed under the GNU General Public License, version 3