Setting up Racket Development in Emacs
All the good ideas never lie under one hat.
In this article, I’ll discuss the easiest approach that I took to setup up a Racket development environment in Emacs. Take note, that this is not the only approach available—some did it in arguably better ways. In this article, I’ll try to explain the shortest route that I took.
Table of contents
Editing Racket code with Emacs has traditionally been done by rudimentary modes that lacked flexibility. They were able to evaluate current definitions, last definitions, and entire buffers, for the most part. Unfortunately, that didn’t suffice with the way Racket dealt with things. A more intelligent way of handling code, was needed.
Fortunately, there is Geiser. There are other major modes that try to do what Geiser does, but I became most comfortable with what Geiser offered. Some similar libraries can co-exist with Geiser, too. I tried those, but it became too complex, for me. I wound up just using Geiser. Also, as a semi-related note, I’m using Emacs to edit Racket code because I don’t know of any other editor that does it so well. I don’t use DrRacket, except when I need to use its nice GUI debugger.
With ELPA, installing Geiser is a breeze. Simply execute the following
M-x package-install RET geiser RET
And in a few seconds, you’ll have Geiser installed in your Emacs profile. Next, you’ll put in the actual code that invokes and configures Geiser:
(require 'geiser) (setq geiser-active-implementations '(racket)) (defun geiser-save () (interactive) (geiser-repl--write-input-ring))
The first expression loads Geiser, itself. The second one specifies that it won’t prompt you for other implementations if it finds them. The last one is optional—it enables you to execute
M-x geiser-save RET
in the REPL buffer to force saving of the history to the disk file, which is found in
~/.geiser_history.racket, by default. It is useful if you want to save your REPL session, immediately. Nothing is more horrifying than losing THAT expression. For all the Emacs code above, to take effect, you can evaluate them now using members of the EVAL troupe—
eval-region—or, you can still opt to respawn a new Emacs process.
To reap what you sowed, create or open a
.rkt file, with at least a proper module declaration. Then hit:
M-x run-geiser RET
And, boomshakalaka! A new (Emacs) window opens, containing the
* Racket REPL * buffer. Whatever you can do with the REPL invoked with vanilla command-line
racket, you can also do with this, and more. This major mode is actually Comint mode, under the hood, with hooks to a Racket process. For those of you who are unfamiliar with Comint mode, it is the same mode that handles
M-x shell RET.
So, what can you do with it? While editing
.rkt file, here are some of the usual shortcuts that I use. The full list of keys are available here. Take note, that the description of the keys that I use below, are for myself initially, to help me understand what they do. They may, or may not diverge from the official description, listed on the aforementioned link.
|Key||What it does|
|C-c C-z||Switch to the REPL buffer|
|C-c C-a||Evaluate current buffer, then switch to the REPL buffer|
|C-M-x||Evaluate current expression|
|C-x C-e||Evaluate last expression|
|C-c C-r||Evaluate region|
|C-c C-\||Insert a lambda (λ) symbol|
|Key||What it does|
|C-c C-z||Switch to the Racket buffer|
|M-p||Switch to the previous history item|
|M-n||Switch to the next history item|
|C-c M-p||Jump to previous prompt|
|C-c M-n||Jump to next prompt|
|C-c C-q||Quit the REPL|
I have intentionally skipped many topics from the official document because it makes it unattractive to people who are averse to reading long blocks of text. Ironically, this article may even qualify as one. The methods described above are by in no way representative of community-advised ways of installing and using Racket with Emacs. Ciao!