Mark Needham

Thoughts on Software Development

Emacs/Clojure: Starting out with paredit

with 10 comments

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:

Screen shot 2012 10 31 at 08 16 18

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:

Screen shot 2012 10 31 at 08 23 15

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:

Screen shot 2012 10 31 at 08 25 16

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:

Screen shot 2012 10 31 at 08 26 04

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 ‘)’:

Screen shot 2012 10 31 at 08 23 15

We can then press ‘Ctrl + Shift + Right Arrow’ to push the right bracket to the end of the line:

Screen shot 2012 10 31 at 08 32 07

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:

Screen shot 2012 10 31 at 08 34 31

And we put our cursor just after the ‘)’ of the ‘+ 5′ bit of code and press ‘Ctrl + Shift + RIght Arrow’:

Screen shot 2012 10 31 at 08 35 24

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?

Be Sociable, Share!

Written by Mark Needham

October 31st, 2012 at 8:41 am

Posted in Clojure

Tagged with , ,

  • Anonymous

    Hi Mark,

    Did you see my paredit talk at the London user group?

    Lots of useful shortcuts demoed.

    http://sw1nn.com/talks/paredit

  • http://twitter.com/Marutks Maris Orbidans

    C-left and C-right moves paren which is on right from your cursor  and
    C-M-left and C-M-right moves paren which is on left from your cursor

  • http://twitter.com/ralph_moeritz Ralph Möritz

    Nice video, thanks!

  • Chris

    META-S is a good one. Turns this:

    (a b (¦c d))

    into this:

    (a b¦c d)

  • http://twitter.com/brandonmhenry bhenry

    whoa. i’ve been using totally different controls than everyone else to do the same things. 

    C-) C-( slurp right and left respectively
    C-] C-[ barf right and left respectively
    M-r raise a single s-expression out of the current s-expression and trash the rest
    M-s as Chris has demonstrated

  • Grant Rettke

    There is paredit-wrap-round that would work like this:

    (defn add [a b]  |(+ a b))

    Run it M:S:(

    (defn add [a b]  (|(+ a b)))

    Type + 5

    (defn add [a b]  (+ 5 |(+ a b)))

  • http://www.markhneedham.com/blog Mark Needham

    @sw1nn:disqus no I hadn’t seen it, am having a look now, thanks!

  • http://www.markhneedham.com/blog Mark Needham

    Thought I’d record a tweet my colleague Phil Potter sent to me about what to learn next:

    @markhneedham after slurp you want barf C-. My favourite by far is M-, splice-sexp-killing-backward. Useful for removing old lets

    https://twitter.com/philandstuff/status/263564981629485056

  • Dennis Roberts

    For clojure, paredit-wrap-square and paredit-wrap-curly are nice to have also.  They’re not bound to any keys by default, but you can add keybindings in your .emacs file.  Here are the keybindings that I use:

    (add-hook ‘clojure-mode-hook ‘(lambda () (define-key clojure-mode-map “M-[” ‘paredit-wrap-square)))

    (add-hook ‘clojure-mode-hook ‘(lambda () (define-key clojure-mode-map “M-{” ‘paredit-wrap-curly)))

  • http://www.markhneedham.com/blog Mark Needham

    @twitter-172214257:disqus I think my emacs has different key bindings than yours – when I try C-) it ends up outputting a ’0′ instead of slurping!