Paredit Slurp and Barf Mnemonics
Of the several sophisticated Paredit commands to edit s-expressions, I believe, the following four commands are the most important ones:
paredit-backward-slurp-sexp
paredit-forward-slurp-sexp
paredit-backward-barf-sexp
paredit-forward-barf-sexp
These commands are bound to the key
sequences C-(
, C-)
, C-{
,
and C-}
, respectively, by default on Emacs. These
commands can make editing s-expressions very convenient. However,
often on IRC networks and other online forums, I come across users
who have difficulty remembering the key sequence each command is
bound to. I will share some mnemonics in this post that make this
easier to remember. Before discussing the mnemonics, let us see
what these commands do. However, if you already know what these
commands do, skip right ahead to the
Mnemonics section.
Slurp Demo
Say, we have the following Lisp expressions in our editor:
(* 10 20) (+ 30 40)
The block above shows where the cursor is placed. Let us now bring
the expression (+ 30 40)
inside the expression (*
10 20)
. To do so, we first place the cursor somewhere inside
the first expression, say, as shown below:
(* 10 20) (+ 30 40)
Now type C-)
and the closing parenthesis of the first
expression moves forward to slurp the next expression. The
result looks like this:
(* 10 20 (+ 30 40))
The key sequence C-)
invokes
the paredit-forward-slurp-sexp
command that adds the
expression following the current list into that list by moving the
closing delimiter ahead. Similarly, there is the key
sequence C-(
that invokes
the paredit-backward-slurp-sexp
command which works
similarly except that it makes the current list consume the
preceding expression. Note that the slurp commands expand the
current list to consume a neighbouring expression.
Barf Demo
Say, we have the following Lisp expression in our editor:
(* 10 20 (+ 30 40))
We now want to move (+ 30 40)
out of the first
expression. To do so, we first place the cursor somewhere inside
the outer expression but not inside the inner expression. Here is
an example of where we place the cursor:
(* 10 20 (+ 30 40))
Now type C-}
and the closing parenthesis of the outer
expression moves behind to barf the inner expression out.
The result looks like this:
(* 10 20) (+ 30 40)
The key sequence C-}
invokes
the paredit-forward-barf-sexp
command that removes the
last expression in the current list from that list by moving the
closing delimiter behind. Similarly, there is the key
sequence C-{
that invokes
the paredit-backward-slurp-sexp
command which works
similarly except that it removes the first expression from the
current list. Note that the barf commands shrinks the current list
to remove an expression from that list.
Mnemonics
When I began using Paredit for the first time, several years ago, I could never remember which key sequences slurp and which ones barf. Then I made up these mnemonics:
-
C-(
andC-)
have parentheses that look nice and round. They expand the current list to consume another expression. Nom nom! -
C-{
andC-}
have braces that look squiggly and wiggly. They shrink the current list and barf an expression.
I do not need these mnemonics anymore because after using these key sequences a few times, they get burnt into our muscle memory. However, in the initial days of using Paredit, these mnemonics were quite useful. Now whenever I find someone complaining about how easy it is to forget the key sequences bound to these commands, I share these mnemonics with them and they never have any problem again with the slurp and barf commands. These mnemonics have served me and many others I know quite well.
Update on 01 Mar 2022: Jon Snader, who maintains a very popular blog named Irreal, has discussed this blog post on his own blog. While he did not find my mnemonics particularly helpful, he has shared his own way of remembering the key sequences. Quoting Jon below:
To me, the braces suggest pointing out so Ctrl+} and Ctrl+{ are clearly for barfing.
That is quite an interesting and elegant mnemonic. See his post Using Paredit’s Slurp and Barf to read his complete views on this topic.