I’ve been complaining recently to Jen and Bruce about the lack of a beginner’s guide to emacs paredit mode which seems to be the defacto approach for people working with Clojure and both pointed me to the paredit cheat sheet.
While it’s very comprehensive, I found that it’s a little overwhelming for a complete newbie like myself.
I therefore thought it’d be useful to write a bit about a couple of things that I’ve picked up from pairing with Jen on little bits of Clojure over the last couple of months.
Let’s say we start with a simple function to add two numbers together:
And say for example we decide that we want to add 5 to the result so the function adds the two numbers together and then adds 5.
Jen showed me that the best way to do this is to go beyond the furthest bracket to the left and start typing there:
The brackets are now a bit misaligned. We need the ‘)’ where the cursor currently is to go to the end of the line.
One way to do this is to move the cursor in front of the ‘(‘ of the second ‘+’ on the line and press ‘Ctrl + K’ which in emacs means ‘kill line to end’ but in this case kills to the end of the expression that we’re at the beginning of:
We then move the cursor back to just after the ‘5’ and press ‘Ctrl + Y’ which in emacs means re-insert the last text that was killed:
This works but it’s a little bit long winded and Jen showed me a quicker way.
If we go back to the position where we had just inserted the ‘+ 5’ and place our cursor just in front of the ‘)’:
We can then press ‘Ctrl + Shift + Right Arrow’ to push the right bracket to the end of the line:
From what I can tell, this can also be achieved by pressing ‘Meta + X’ followed by ‘paredit-forward-slurp-sexp’ or ‘Meta + Shift + )’.
We have to be a little bit careful about where we position the cursor because if we put it after the bracket then we can end up bringing another function into our one by mistake!
For example say just below our ‘add’ function we have a subtract one:
And we put our cursor just after the ‘)’ of the ‘+ 5’ bit of code and press ‘Ctrl + Shift + RIght Arrow’:
We now have a bizarre looking function ‘add’ which has the ‘subtract’ function contained inside it!
At the moment this is the main paredit shortcut I know and it seems to work reasonably well. I also find myself using ‘Ctrl + Shift + -‘ which allows me to undo any mistakes I make!
Now to learn my next command! Any suggestions?