A Clojure Beginner's Guide to Structural Editing with Paredit

Paredit is a tool to help with structural editing. It helps you write and modify S-Expressions with so much ease that the brackets will almost disappear! In this chapter, we'll learn fundamental Paredit commands.

Paredit#

Paredit helps with structural editing. In non-jargon terms, it's a tool to aid the writing of S-Expressions. Since Paredit is dependent on the editor you use, we'll only cover the core features in this chapter and share links to editor specific resources at the end.

The presentation style of this chapter was inspired by Dan Midwood's Animated guide to Paredit.

Paredit is an indispensable part of a Lisper's toolkit. This chapter is a cheat sheet that you can refer back to as you progress in the course.

We installed Paredit in the Getting Started section. If you are a VS Code user, you don't need to install Paredit separately as it comes bundled with Calva.

We are going to reuse the first-project.core namespace to test various Paredit features. We suggest you follow along!

Bracket pair balancing#

You have probably observed this already - when you open a bracket, Paredit automatically adds the closing bracket. The same is true when you delete a bracket - the closing part is deleted automatically.

Bracket pair balancing

Delete protection#

Paredit prevents accidental deletion of brackets by only allowing you to delete leaf nodes of the S-Expression. If you try to delete a parent node, the cursor just moves back.

Delete protection

Notice how the cursor just moves back when I press backspace on a form with children on line 13, whereas the forms which don't have any children (ie child forms) are deleted on line 15.

Delete protection helps as your forms become more complex. At first, you might find it a bit annoying, but you'll soon get used to it.

Forward and backward Slurp#

Slurp engulfs a form into a list. Forward Slurp engulfs the form immediately next to the end of the list whereas backward Slurp engulfs a form immediately preceding a list.

Slurp

The keyboard shortcuts for Slurp depends on the editor you are using. We have noticed that these shortcuts sometimes conflict with the OS or existing shortcuts. You might need to rebind some commands depending on your setup (especially if you are using Mac). The default shortcuts are:

 

This page is a preview of Tinycanva: Clojure for React Developers

Start a new discussion. All notification go to the author.