Buffer Yard
Language setup

Emacs Racket Setup: racket-mode, the REPL and Paredit

Updated

You type (define (square x) (* x x)) into a buffer, run it, and want to ask the program a question: what does (map square '(1 2 3)) return? In DrRacket you would click Run and type into the bottom pane. In Emacs the equivalent is one command, racket-run, and the reason it feels so close to DrRacket is that racket-mode was written to give Emacs users that same edit, run, explore loop.

Why not just use DrRacket

DrRacket is a good program, and for teaching it is hard to beat. Its Check Syntax draws arrows from each use of a name to its binding, its stepper shows evaluation one reduction at a time, and its error messages are tuned for students of the How to Design Programs curriculum. It also ships with Racket, so there is nothing to configure.

The reasons to switch are the usual Emacs reasons. Your keybindings, your completion UI, version control through Magit, the same window layout you use for Python and C. Once Racket stops being the only thing you write, a second editor with its own habits gets in the way.

The stepper is the one feature with no real Emacs counterpart. If you are working through a textbook that leans on it, keep DrRacket around for that.

racket-mode

racket-mode is written and maintained by Greg Hendershott and is available from MELPA and NonGNU ELPA. It is more than a major mode: it starts a Racket process as a back end and asks it questions, which is how it knows about modules, bindings and documentation without regular-expression guesswork.

Racket itself has to be installed first, from racket-lang.org or your package manager. Then:

(use-package racket-mode
  :ensure t
  :hook (racket-mode . racket-xp-mode))

That is the whole minimal setup. racket-xp-mode is the part that brings Check Syntax into Emacs: it annotates the buffer with definitions and uses, highlights unused requires, shows errors as you type and lets you jump to a binding. The first time you open a file it runs the expansion in the background, so annotations appear after a short delay.

The REPL, through racket-run

racket-run saves the buffer, runs the module in the back end and opens a REPL buffer whose namespace is the inside of that module. Everything you defined is available there, including names you did not provide. That last detail is what makes it useful while developing: you can call helper functions directly without exporting them.

Each run starts from a fresh namespace. Definitions you typed only in the REPL disappear on the next run, which is intentional (DrRacket behaves the same way) and prevents the slow drift between what your file says and what your session contains.

A few related commands worth knowing by name:

  • racket-run-module-at-point runs the innermost module or module+ form, handy for running a test submodule
  • racket-test runs the test submodule and reports results
  • racket-xp-describe shows documentation for the identifier at point inside Emacs (with racket-xp-mode on)
  • racket-repl switches to the REPL buffer without rerunning

Look at the mode’s keymap with C-h m in a Racket buffer to see where these sit; racket-mode’s defaults deliberately echo DrRacket in a few places.

Or use Eglot instead

If you would rather treat Racket like every other language in your config, racket-langserver provides LSP support. Install it with raco:

raco pkg install racket-langserver

Eglot already maps racket-mode to racket -l racket-langserver, so the Emacs side is one hook:

(use-package racket-mode
  :ensure t
  :hook (racket-mode . eglot-ensure))

The server uses the same Check Syntax machinery underneath, so the information is similar. Do not run both racket-xp-mode and Eglot in the same buffer; you would get two sets of diagnostics for one problem. Racket-mode with xp is the richer option, but the Eglot route keeps your package list and keybindings uniform across languages. racket-run works either way, because the REPL comes from racket-mode, not from the server.

Structural editing

This is the section that matters most once you write more than a few lines. Typing parentheses one at a time and counting them is how Lisp gets its reputation. Structural editing removes that problem by making the editor treat expressions, not characters, as the thing you edit.

Emacs already has some of this built in. show-paren-mode is on by default since 28, electric-pair-mode inserts closing brackets, and the C-M- motion commands (C-M-f, C-M-b, C-M-k) move and kill whole expressions in any Lisp buffer. That is enough to start.

Paredit goes further and refuses to let you unbalance the buffer. The two commands to learn first are slurp and barf. With point inside (foo bar) baz, C-) pulls baz into the list; C-} pushes the last element back out. M-s splices a list into its parent and M-r raises the expression at point, replacing its parent. Those four cover most restructuring.

Smartparens handles the same ideas with a broader scope: it works in non-Lisp modes too and can enforce strict balance through smartparens-strict-mode.

Lispy, by Oleh Krehel, takes a different approach: when point is directly before or after a parenthesis, single letters become commands, so j and k move between expressions without a modifier key. Fast once learned, disorienting before that.

(use-package paredit
  :ensure t
  :hook ((racket-mode racket-repl-mode emacs-lisp-mode) . paredit-mode))

Adding the hook to emacs-lisp-mode as well is deliberate. Your own init.el is Lisp, and you will practise slurping and barfing there every day.

Other pages

If you came to Racket from a typed functional language, the Haskell setup shows the GHCi equivalent of this REPL loop. The Elixir page covers another language where sending code to a running session is the normal way to work. Completion inside the REPL and source buffers uses completion-at-point, so the front end from the completion guide applies here unchanged, and every other language is on the languages index.

#lang lines other than racket (Typed Racket, Scribble, a teaching language) run through racket-run as well, since the back end hands the module to Racket as is.

Questions readers ask

Do I need a language server for Racket in Emacs?

No. racket-mode talks to its own Racket back end, and racket-xp-mode already gives you definitions, binding arrows and errors as you type. racket-langserver through Eglot is an alternative if you prefer the same LSP workflow you use for other languages.

Should a beginner use paredit or smartparens?

Paredit if you write mostly Lisp and Racket, because it is strict and its commands are few. Smartparens if you also want pair handling in other languages, since it covers quotes, brackets and markup tags beyond s-expressions.

Why does racket-run complain that it cannot find racket?

racket-mode looks for the racket executable using racket-program, which defaults to finding it on exec-path. Either add Racket's bin directory to exec-path or set racket-program to the full path of the executable.