My Smartparens and Elixir setup

As you all could imagine I write a great amount of Emacs Lisp because for Alchemist. To get a bit of support while writing all these parentheses I use a minor mode called Smartparens.

It quotes itself as the following:

... deals with parens pairs and tries to be smart about it.

For someone who writes Emacs Lisp regularly the Smartparens also offers great functionality to move between expressions for example, which are really handy.

But I guess for you Elixir guys this isn't what you looking for. So what I like about Smartparens too is the ability to add new custom pairs, like in our situation as Elixir programmers do/end for example.

There are two situations where I like the auto pairing, in do/end or ->/end cases.

Let's see how my custom setup looks like to realize the desired behavior:

(defun my-elixir-do-end-close-action (id action context)
  (when (eq action 'insert)
    (forward-line -1)

(sp-with-modes '(elixir-mode)
  (sp-local-pair "->" "end"
                 :when '(("RET"))
                 :post-handlers '(:add my-elixir-do-end-close-action)
                 :actions '(insert)))

(sp-with-modes '(elixir-mode)
  (sp-local-pair "do" "end"
                 :when '(("SPC" "RET"))
                 :post-handlers '(:add my-elixir-do-end-close-action)
                 :actions '(insert)))

First, we add the new pairs just for the elixir-mode major mode. Secondly we define our desired start and end pairs, in our case we start with "->" / "end". The next thing we need to inform Smartparens about is the keys which should trigger the closing action. For the first pair definition, these would be just "RET" (Return). For a correct indentation after our favored pairing is done, we add our custom function my-elixir-do-end-close-action to the post handlers. These functions will be called by Smartparens after the pairing is done, in our case we just obtain our desired indentation.

That's all, you now will have these two auto pairings available inside your Elixir files.

I'm not sure about any other pairings which would be useful, if you have some ideas I would be glad to hear it.


As Martin Gausby mentioned in the comments, set the idly time delay for company-mode a bit higher would be a good idea to get around the issue that company-mode will complete functions like do_* before you even could press ENTER to trigger the end part of the do / end pairing.

That's how you change the idly delay time for company-mode completion:

(setq company-idle-delay 0.5)