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 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.
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.
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.
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
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.
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: