Sigils

Contents
~w word lists
~r regex
~s strings
Lowercase escapes

~w word lists

The sigil ~w builds a list of words without writing quotes and commas around every item. Words split on whitespace, so one short sigil replaces a long literal list. Adding the a modifier turns each word into an atom instead of a string.

words = ~w(apple banana cherry) IO.inspect(words) tags = ~w(red green)a IO.inspect(tags)

["apple", "banana", "cherry"]
[:red, :green]

~r regex

The sigil ~r builds a regular expression without doubling backslashes. Any delimiter works, so parentheses keep the pattern readable when the pattern itself holds slashes. The Regex module then tests strings against the compiled pattern.

pattern = ~r(^hello) IO.inspect(Regex.match?(pattern, "hello there"))

true

~s strings

The sigil ~s builds a string with interpolation and escape sequences turned on. It shines when the text holds double quotes, since parentheses delimit the string instead of quotes. The uppercase twin ~S keeps everything literal, so interpolation marks stay untouched.

name = "Ada" IO.puts(~s(hello #{name})) IO.puts(~S(hello #{name}))

hello Ada
hello #{name}

Lowercase escapes

A lowercase sigil name keeps interpolation and escape sequences active, while the uppercase form switches both off. So ~s interpolates and ~S stays literal, and the same split holds for ~w with ~W and ~r with ~R. Delimiters stay flexible as well: parentheses, brackets, braces, and pipes all work, so pick a pair that stays out of the text.

IO.puts(~s(#{1 + 1})) IO.puts(~S(#{1 + 1})) IO.inspect(~w[a b c])

2
#{1 + 1}
["a", "b", "c"]

Next: Binaries and charlists

Article author: Arthur Isaev

Related articles
Elixir
Elixir course: zero to hero in 50 lessons
What is Elixir and where it runs
Install Elixir
iex: the interactive shell
Your first script
Variables
Numbers
Strings
Atoms
Truthiness: only false and nil
Conditions
Recursion replaces loops
Lists
Tuples
Maps
Keyword lists
Functions
Defaults and clauses
Pattern matching
case
with chains
Modules in Elixir
import, alias, require, use in Elixir
Structs in Elixir
Protocols in Elixir
Behaviours in Elixir
The pipe operator in Elixir
Enum in Elixir
Stream in Elixir
Comprehensions in Elixir
Advanced strings in Elixir
Sigils
Binaries and charlists
Exceptions
ok and error tuples
Files and IO
Processes
GenServer
Supervisors
Tasks and Agents
OTP applications
Mix projects
Dependencies
Testing with ExUnit
Doctests
Debugging
Configs
Releases
Capstone: todo CLI
Capstone: HTTP server
Hero roadmap: the whole course on one page
Install Elixir on Windows 11
Install Elixir on Ubuntu
Install Elixir on Rocky Linux
Install Elixir on macOS
Install Elixir on FreeBSD
Elixir in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: