Sigils

A sigil is a literal with a tag in front: a tilde, a letter, and a pair of delimiters, with optional letters after the closing delimiter. The letter says what the text means and the delimiters say where it ends, so a string full of quotes or a regular expression full of slashes stays readable. In Elixir the lowercase tag interpolates and the uppercase tag does not.

~s and ~S

~s is a string with another delimiter, and it interpolates like a double-quoted string. ~S is the raw version: the text stays exactly as written, so a template that contains #{ can be held without escaping anything:

strings.exs

name = "Ada" IO.puts(~s(hello #{name})) IO.puts(~S(hello #{name})) IO.inspect(~s|a "quoted" word|)

elixir strings.exs

hello Ada
hello #{name}
"a \"quoted\" word"

The third line uses the pipe as its delimiter, which is the reason sigils exist: the text holds double quotes and no backslash is needed. Any pair of non-alphanumeric delimiters works, and (, [, { and < may nest with their closing partner. The inspector still shows the value as a normal string, because a sigil is only a way to write one.

~w and ~c

~w splits its text on whitespace into a list of strings, and the a modifier makes the items atoms instead. ~c is a charlist, the list of code points that the Erlang functions take:

chars.exs

IO.inspect(~c"abc") IO.inspect(~w(apple banana cherry)) IO.inspect(~w(apple banana cherry)a) IO.inspect(~c"abc" ++ ~c"def")

elixir chars.exs

~c"abc"
["apple", "banana", "cherry"]
[:apple, :banana, :cherry]
~c"abcdef"

The inspector prints a charlist as ~c"abc" rather than as a list of numbers, which is the same choice it makes for a string. The a modifier is the shortest way to write a fixed list of atoms, and it is the form a list of options or states takes in a test.

~r and its modifiers

~r builds a regular expression, and the modifiers after the delimiter change how it matches: i ignores case, m makes ^ and $ match at every line, s lets a dot match a newline, and u turns on the Unicode rules:

regex.exs

IO.inspect(Regex.run(~r/\d+/, "abc 123")) IO.inspect(Regex.scan(~r/\d+/, "1 22 333")) IO.inspect(Regex.replace(~r/\s+/, "a b", " ")) IO.inspect(Regex.run(~r/abc/i, "ABC"))

elixir regex.exs

["123"]
[["1"], ["22"], ["333"]]
"a b"
["ABC"]

The slashes are the usual delimiter and the backslash of \d is written once, because the sigil does not read it as an escape of its own. A pattern that contains a slash takes another delimiter, ~r{...}, and the i modifier is what makes the last line match upper case.

~D, ~T and ~U

Three sigils build the date and time structs of the standard library at compile time, which is the shortest way to write a fixed moment in a test or a default:

dates.exs

IO.inspect(~D[2026-11-28]) IO.inspect(~T[12:30:00]) IO.inspect(~U[2026-11-28 12:30:00Z])

elixir dates.exs

~D[2026-11-28]
~T[12:30:00]
~U[2026-11-28 12:30:00Z]

~D is a Date, ~T a Time and ~U a DateTime in UTC. A date read from the outside world is parsed instead, with Date.from_iso8601/1, because a value from a file cannot be checked by the compiler.

The mistake: a modifier the sigil does not take

The modifiers are not free letters: each sigil accepts a list of its own and refuses the rest. ~w takes s, a and c, and an x after it stops the program with an ArgumentError that names the three it knows:

mistake.exs

IO.inspect(~w(apple banana)x)

elixir mistake.exs

** (ArgumentError) modifier must be one of: s, a, c
    (elixir 1.19.7) lib/kernel.ex:7001: Kernel.split_words/3
    (elixir 1.19.7) expanding macro: Kernel.sigil_w/2
    mistake.exs:1: (file)

The second line of the trace is the expansion of the sigil, which is where the check happens. The fix is one of the three the message names, and a sigil with no modifier at all is the common case: the letters are only there when the value has to change.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is the difference between ~s and ~S?

    Answer

    ~s interpolates and reads escapes; ~S keeps the text as written

    The lowercase tag is the normal string and the uppercase tag is the raw one, as with ~r and ~R.

  2. What does the a modifier do to ~w?

    Answer

    It makes the items atoms instead of strings

    The a modifier turns each word into an atom, which is the shorter form of a list of atoms.

  3. What does ~c"abc" build?

    Answer

    a charlist, the list of code points

    The c sigil is the charlist, which the Erlang functions take and an inspector shows as ~c.

  4. Why does a sigil let the delimiters be chosen?

    Answer

    So the text does not have to escape the delimiter it contains

    A pattern full of slashes or a string full of quotes reads better with another pair of delimiters.

  5. What does this program print? name = "Ada" IO.puts(~S(hello #{name}))

    Answer

    hello #{name}

    The uppercase sigil is raw, so the interpolation stays as text.

  6. What does this program print? name = "Ada" IO.puts(~s(hello #{name}))

    Answer

    hello Ada

    The lowercase sigil interpolates like a double-quoted string.

  7. What does this program print? IO.inspect(~w(red green blue)a)

    Answer

    [:red, :green, :blue]

    The a modifier gives one atom per word, and the inspector prints the list.

  8. What does this program print? IO.inspect(Regex.run(~r/\d+/, "a 42 b"))

    Answer

    ["42"]

    run returns the first match as a list of strings, and the digits are the match.

  9. What does this program print? IO.inspect(Regex.replace(~r/\s+/, "a b", " "))

    Answer

    "a b"

    The call returns a string with the run of spaces replaced, and inspect shows the quotes.

  10. What does this program do? IO.inspect(~w(apple banana)x)

    Answer

    An ArgumentError, because ~w takes only s, a and c

    The sigil refuses a modifier outside its set and the error names the three it accepts.

Автор статьи: Arthur Isaev