search


Clojure Development with Emacs
Tue Jun 12 00:00:00 UTC 2018

One of the biggest complaints about programming in Clojure or any LISP dialect is keeping those damn parenthesis balanced. However, a text editor that understands how to format and manipulate s-expressions eliminates this problem.

What is structural editing?

Structural editing is manipulation of code as data structures and not simple strings. Emacs with paredit plugin is a structural editor. I will use emacs to teach but you can use any text editor that can manipulate s-expressions. Intellij with Cursive plugin is another editor that can manipulate s-expressions. Without a good structural editor, writing LISP code is painful and frustrating

Starting emacs
% emacs

The first time you start emacs, it will install packages specified in ~/.emacs.d/init.el and you will see some warnings. Ignore the warnings. If you get errors exit emacs and start emacs again. To exit emacs, use the keyboard shortcut ctrl-x ctrl-c. This means keep finger on ctrl key while pressing x and then c key. You may need to exit and restart emacs several times before all packages are installed without errors.

All of emacs plugins are actually written in a dialect of LISP called emacs LISP. Emacs is a LISP Machine implemented in software

Cider

Cider is an emacs plugin making Clojure development easier by interfacing emacs to the REPL. Refer to Cider documentation for more details

Paredit

Paredit is an emacs plugin that does structual editing of s-expressions. There are others like parainfer and smarparens . I recommend Paredit only because its what I'm most familar with and works for me. Feel free to explore other options. Using Paredit (or any structural editor) well is key to making programming in a LISP efficient otherwise you will get lost in the parenthesis. Here are some resources on Paredit:

Magit

Magit is an git plugin for emacs. Here's a Magit Cheatsheet

Frequently used keyboard shortcuts

In table below, C stands for the Ctrl key. M stands for Meta key which is usually bound to the the ESC or ALT key

Action Keyboard Shortcuts
Start a shell within emacs M-x shell
Exit C-x C-c
Open File C-x C-f
Find a file in project M-x ffip
Mark a region C-space
Copy a text region into memory M-w
Cut line/s-expression C-k
Paste memory C-y
Move down a line C-n
Move up a line C-p
Move to beginning of line C-a
Move to end of line C-e
Move to next s-expression M-f
Move back to previous s-expression M-b
Cut an s-expression C-k
Format code selection C-A-\
Evaluate s-expression C-x C-e
Evaluate buffer C-c-k
Switch namespace C-c M-n
Switch Buffer C-b
Move Cursor between Windows C-x o
Split Buffer Vertically M-1
Split Buffer Horizontally M-2
Move between windows C-x o
Start and connect to REPL (jack in) M-x cider-jack-in
Slurp an s-expression C-right-arrow
Barf an s-expression C-left-arrow
Delete an s-expression C-d
Resize windows using doremi C-x t w
Official Emacs Cheatsheet