Skip to content

Sigils ​

Source: Elixir guide, Sigils.

Sigils give the language custom textual literals. A SigilA custom textual representation. It starts with ~, then a letter and a delimiter, with optional modifiers after the last delimiter. You can define your own sigils.Read the page · Glossary starts with ~. Then it has one lowercase letter or one or more uppercase letters, an opening delimiter, the content, a closing delimiter, and optional modifiers.

~r/foo/i is short for a call to sigil_r/2.

Regular expressions ​

~r builds a PCRE regular expression. Modifiers go after the last delimiter:

elixir
"foo" =~ ~r/foo|bar/    #=> true
"HELLO" =~ ~r/hello/    #=> false
"HELLO" =~ ~r/hello/i   #=> true

You can use eight delimiters: / | " ' ( ) [ ] { } < >. Pick one that avoids escapes. ~r(^https?://) is easier to read than ~r/^https?:\/\//.

Text sigils ​

SigilBuildsExample
~sa string~s(has "double" quotes)
~ca CharlistA list of code points, written ~c"like this". It is mostly for interoperability with Erlang.Read the page · Glossary~c(cat)
~wa list of words~w(foo bar bat) gives ["foo", "bar", "bat"]

~w takes the modifiers c, s, or a for the type of the elements: ~w(foo bar bat)a is [:foo, :bar, :bat].

Lowercase and uppercase ​

Most lowercase sigils have an uppercase twin. The lowercase sigil handles escapes (\n, \x26, \u{1F600}, and others) and #{} interpolation. The uppercase sigil takes the text as it is.

Use ~S when you write escapes in docs.

These are the escape codes in textual sigils:

CodeMeaning
\\single backslash
\abell/alert
\bbackspace
\ddelete
\eescape
\fform feed
\nnewline
\rcarriage return
\sspace
\ttab
\vvertical tab
\0null byte
\xDDone byte in hex, such as \x13
\uDDDD, \u{D...}a Unicode code point in hex, such as \u{1F600}

Heredocs (""") work with sigils. For this reason, @doc ~S""" prevents double escapes in documentation.

Calendar sigils ​

SigilBuilds
~D[2019-10-31]%Date{}
~T[23:00:07.0]%Time{}
~N[2019-10-31 23:00:07]%NaiveDateTime{}, with no time zone ("naive")
~U[2019-10-31 19:59:03Z]%DateTime{} in UTC

Custom sigils ​

Define sigil_{char} functions. The content arrives as a string and the modifiers arrive as a charlist:

elixir
defmodule MySigils do
  def sigil_i(string, []), do: String.to_integer(string)
  def sigil_i(string, [?n]), do: -String.to_integer(string)
end

import MySigils
~i(13)    #=> 13
~i(42)n   #=> -42

A custom sigil is one lowercase letter, or an uppercase letter that is followed by uppercase letters and digits. People often use sigils to embed templating languages or other small languages.

Site code: MIT. Pages and diagrams come from the Elixir documentation (Apache-2.0, snapshot of 2026-10-08, Elixir 1.20). This project is not affiliated with the Elixir Team.