I’m using Elixir more and more lately and I love it!

Elixir is a functional language and as such it’s very common to feed a function with the return of another one like so:

length(String.split(line))

It can quickly become are to read so Elixir provides a syntactic sugar to pipe a function return to another one:

line
|> String.split()
|> length()

Easier on the eyes isn’t it?

I like this syntactic sugar a lot but I hate to type it. It’s not an easy one on azerty or bépo layouts.

As an Emacs user I can finely customize everything so I decided to pimp the elixir-mode to make the pipe operator easy to use.

Main function

First of all we need a function that will describe what we want to do:

(defun bounga/insert-elixir-pipe-operator ()
  "Insert a newline and the |> operator"
  (interactive)
  (end-of-line)
  (newline-and-indent)
  (insert "|> "))

We defined the function bounga/insert-elixir-pipe-operator that:

  • acts interactively
  • goes to the end of the current line
  • adds a newline and indents the cursor
  • inserts the pipe operator followed by a white space

If you try it by calling M-x bounga/insert-elixir-pipe-operator in a buffer, you’ll see it does what we want.

Bind the function to a key chord

To use this new function effectively you should bind it to a key chord (a keyboard shortcut).

We’ll bind the function to M-RET (Alt + Enter on most keyboards).

Depending on the way you handle your packages there’s two ways of setting it up.

Vanilla Emacs

(define-key elixir-mode-map (kbd "M-RET") 'bounga/insert-elixir-pipe-operator)

use-package

(use-package elixir-mode
  :bind (:map elixir-mode-map
              ("M-RET" . bounga/insert-elixir-pipe-operator)))

Now you’re good to go!

If you’re in an Elixir buffer then M-RET will insert a new line and add the pipe operator whether you’re at the end of the line, at the beginning or in the middle of it.

Demo

Let’s see it in action:

emacs_elixir_pipe_operator_demo.gif

Have fun with Emacs and Elixir!

Have comments or want to discuss this topic?

Send an email to ~bounga/public-inbox@lists.sr.ht