Developing in Clojure with Emacs: Mastering the Keyboard
As the founder of CloudRepo, one of the questions I’ve had was how the Clojure development experience would be using Emacs and CIDER. I have always been a big fan of IntelliJ with the Cursive Clojure Plugin, but I wanted to make sure I wasn’t missing anything by ignoring Emacs.
I figured that the best way to find out for myself was to try to develop using Emacs and CIDER for a week. I learned a lot in that week but afterwards I decided to go back to IntelliJ and Cursive.
There was a lot I learned during that week.
This is the first in a series of post that will describe the lessons learned from my time developing Clojure apps with Emacs.
The first lesson I learned was that if you want to get full utility out of Emacs, you must first become a master of the keyboard.
Before I go further, I need to share the setup used in the creation of this article.
I used Emacs 24.5.1 with CIDER 0.16.0 on Linux Mint 18.3.
I would have had no clue how to configure a basic install of Emacs to use CIDER if it weren’t for Daniel Higginbotham’s excellent book Clojure for the Brave and True.
Read this book if you’re new to Clojure.
Read the chapter on Developing with Clojure in Emacs if you’re new to Emacs or if you’re new to writing Clojure in Emacs.
The author provides a complete set of Emacs configuration files for CIDER, but these were almost four years old and needed to be updated.
I downloaded the configuration files, upgraded to CIDER 0.16.0 (from 0.8.0), made a couple of other modifications and pushed them up into BitBucket so I could share them between my computers.
If anyone is interested in using my configuration, they’re available in this BitBucket Repository.
Once you’re all configured, you can rejoin us on the path to keyboard mastery.
Now back to it…
Mastering the Keyboard
Forcing myself to use the keyboard for everything was the most difficult part of switching to Emacs.
While IntelliJ can be configured to be completely keyboard driven, it provides the mouse as an escape hatch for when you have no idea of which keys you need to press.
When you have an escape hatch, it’s easy to use. Just right click and you can usually find a menu that has what you are looking for. Problem solved!
The mouse isn’t really an option in Emacs, where it has very limited functionality.
When Emacs first starts up, it will stare at you and await your every command, which usually comes from the keyboard.
At first, in order to figure out what to type, I was constantly jumping between Emacs and various keybinding cheat sheets in order to figure out which keys to press.
Then I discovered
In my configuration,
M-x allows you to search for functions that you want to execute. When I want to find a paredit command, I’ll enter
M-x paredit and then a list of suggested functions will appear in the mini buffer.
If I want to see all matches I can just hit the
I could do this with any prefix or word that I thought might be related to what I was looking for (ie. cider, buffer, previous, next, etc.).
This is how I started to become familiar with the functions available to me.
Additionally, anytime I invoked a function via
M-x the mini buffer would tell me if that function was bound to an existing key sequence.
That really helped me associate functionality with the appropriate key bindings.
Once I learned about
function -> key binding mappings, I started to wonder which functions were being invoked from the key bindings I had already learned to use via accidental key presses and cheat sheets.
Learning the Help System
In order to find out which functions were being invoked by a key sequence (ie.
key binding -> function, I used the
C-h k command. Pressing this then prompts for the key sequence you’re curious about.
Typing the key sequence in the mini-buffer causes the documentation for the bound function to display.
C-h is the prefix you use before invoking many of the help functions.
Another useful command is
C-h f which lets you type in the name of a function and display its documentation.
All that’s pretty great if you know which key sequence you’re using, but what if you don’t know which keys are available to press after entering something like
That’s where the whichkey package comes in.
The whichkey package
The whichkey package provides you with hints as to which keys are available to press along with the functions they will invoke.
When I type
C-x whichkey presents the following:
Wow, made a huge improvement to the usability of Emacs for me. No longer did I have to search for the right key to press. I could start with a prefix and then I could view all the subsequent keys available to me.
When it came to exploring what I could do in CIDER whichkey was invaluable:
With all the options displayed to me for a particular key press, I was free to experiment and investigate functionality.
The Path Continues
Mastering the keyboard isn’t something you can do in a day or a week. It takes months of continual practice and use.
In Emacs, there are so many different combinations of key strokes that it can seem overwhelming at first.
The tools presented in this article provide the ability to explore the key bindings and functions which compose the whole of Emacs.
Start with these tools and you’ll find that traveling the Emacs path will become much less painful.
Keep at it and force your hands to stay on the keyboard and soon keystrokes will be placed into muscle memory and you’ll be executing 7 key combinations so fast your co-workers will wonder if you’re only hitting a single key.