Learn ELVISH with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
1
Elvish Counter and Theme Toggle
count = 0
isDark = $false
fn updateUI {
echo "Counter: $count"
if $isDark {
echo "Theme: Dark"
} else {
echo "Theme: Light"
}
}
fn increment {
count = (math $count + 1)
updateUI
}
fn decrement {
count = (math $count - 1)
updateUI
}
fn reset {
count = 0
updateUI
}
fn toggleTheme {
isDark = (not $isDark)
updateUI
}
# Simulate actions
updateUI
increment
increment
toggleTheme
decrement
reset
Demonstrates a simple counter with theme toggling using Elvish variables and functions.
2
Elvish Fibonacci Sequence
a = 0
b = 1
echo $a
echo $b
for i (1 2 3 4 5 6 7 8) {
c = (math $a + $b)
echo $c
a = $b
b = $c
}
Generates the first 10 Fibonacci numbers.
3
Elvish Factorial Calculator
fn fact [n]{
if (math $n == 0) {
return 1
} else {
return (math $n * (fact (math $n - 1)))
}
}
echo (fact 5)
Calculates factorial of a number recursively.
4
Elvish Prime Checker
fn isPrime [n]{
if (math $n < 2) { return $false }
for i (2..(math $n - 1)) {
if (math $n % $i == 0) { return $false }
}
return $true
}
echo (if (isPrime 13) {"Prime"} else {"Not Prime"})
Checks if a number is prime.
5
Elvish Sum of List
lst = [1 2 3 4 5]
sum = 0
for x $lst {
sum = (math $sum + $x)
}
echo $sum
Calculates sum of a list of numbers.
7
Elvish Multiplication Table
n = 5
for i (1..10) {
echo (str $n " x " $i " = " (math $n * $i))
}
Prints multiplication table of a number.
8
Elvish Celsius to Fahrenheit
c = 25
f = (math ($c * 9 / 5) + 32)
echo $f
Converts Celsius to Fahrenheit.
9
Elvish Simple Alarm Simulation
temp = 80
thresh = 75
echo (if ($temp > $thresh) {"Alarm: Temperature Too High!"} else {"Temperature Normal"})
Simulates an alarm if a threshold is exceeded.
10
Elvish Random Walk Simulation
steps = 10
pos = 0
for i (1..$steps) {
if (random 1 < 0.5) {
pos = (math $pos + 1)
} else {
pos = (math $pos - 1)
}
echo $pos
}
Simulates a 1D random walk.