vi studio

Language guide · for the course, see the lessons

Contents
  1. Comments and names
  2. Containers
  3. Size and space
  4. Arrange and place
  5. Text
  6. Colour, shape and images
  7. Buttons, inputs and other kinds
  8. Events
  9. Data: lists and objects
  10. Functions
  11. Conditions and loops
  12. Programs without a screen
  13. Screens
  14. Containers that take values
  15. Repeating things
  16. Reaching other containers
  17. Changing the screen
  18. Timers and games
  19. Sound and video
  20. Keeping data
  21. The internet and the cloud
  22. The phone
  23. Games
  24. Popups and menus
  25. Pages and tabs
  26. Dates and time
  27. Text and number helpers
  28. Forms
  29. Maps
  30. Purchases and ads
  31. Themes and dark mode
  32. Accessibility
  33. More than one language
  34. JavaScript, for what Vi doesn't cover
  35. Files and projects
  36. The config block
  37. Errors and good habits

The Vi guide

The lessons in Studio's Learn panel are built from this file, one ## section per lesson (docs/tools/build-learn.mjs). Every vi example here is a whole program and runs with Try it; the few marked vi fragment only show one idea. compiler/tests/learn.test.ts checks them all.

Comments and names

A # starts a comment that runs to the end of the line. <# … #> holds a comment over several lines. Vi ignores comments; they are notes for people.

<# A greeting card.
   This comment spans three lines. #>
main greeting_card:
  color: blue[50]          # a light blue background
  arrange: center
  children:
    greeting_text:
      text: "Hello from Vi!"
      text_style: 22

Names are lowercase words joined with underscores: greeting_card, add_task, high_score. A name says what the thing is or does. Vi tells the kinds of names apart by how they are written:

Written like It is Example
name = value a variable (data) score = 0
name(…): + statements a function (work) add_point():
name: + attributes a container (something on screen) header:

Containers

Everything on the screen is a container: a box with attributes, one per line. A container can hold other containers in children:, one per line.

main main_screen:
  color: gray[100]
  children:
    header:
      size: max, 15
      color: purple[400]
      arrange: center
      children:
        title:
          text: "Containers"
          text_style: 20, white
    body:
      size: max, max
      arrange: center
      children:
        note
    footer

note:
  text: "A container defined on its own, used by name"

footer:
  size: max, 10
  color: purple[100]

Several containers can share one style:

main row_of_cards:
  arrange: row, spread
  space_inside: 16
  children:
    card_a
    card_b
    card_c

card_a, card_b, card_c:
  size: 30
  color: teal[300]
  shape: rounded

Size and space

Boxes stretch with the screen; text and gaps stay the same.

size: width, height is out of 100 of the parent: width against the parent's width, height against the parent's height. One number makes a square from the parent's width.

Value Means
size: 50, 20 half as wide as the parent, a fifth as tall
size: 30 a square, 30 % of the parent's width on each side
size: max, auto fill the width, as tall as the content
max fill the room that is left
auto fit the content (the default)

Space is in fixed points, the same on every phone:

Attribute The space…
space_inside inside the container, around its children (padding)
space_between between the children
space_around outside the container (margin)

The difference between space_inside and space_around is which side of the edge the space is on. space_inside is part of the container: its colour, image and tap area fill it, and it pushes the children away from the edge. space_around is outside: the parent shows through, and it pushes the container away from its neighbours and its parent's edge.

One number is every side. Name the sides for different ones, in any combination: space_inside: 16, top 32 or space_inside: left right 20, top bottom 8.

main page:
  space_inside: 24, top 48
  space_between: 12
  children:
    box_a:
      size: max, 20
      color: amber[300]
    box_b:
      size: 50, 20
      color: amber[500]
      space_around: left 40
    box_c:
      size: 25
      color: amber[700]

Arrange and place

arrange: says how a container lays out its children: the direction, the spot and the spread, in any order, all optional.

Vi works out the rest. You never say which axis is "main"; bottom right means the bottom right corner whichever way the children run.

Picture the container as a box and its children as a small stack. The spot says where in the box the stack sits; the first word is up or down, the second is left or right:

 arrange: center         arrange: top center
┌───────────────┐       ┌───────────────┐
│               │       │     [ A ]     │
│     [ A ]     │       │     [ B ]     │
│     [ B ]     │       │     [ C ]     │
│     [ C ]     │       │               │
│               │       │               │
└───────────────┘       └───────────────┘

 arrange: bottom right   arrange: row, center
┌───────────────┐       ┌───────────────┐
│               │       │               │
│               │       │               │
│          [ A ]│       │ [A]  [B]  [C] │
│          [ B ]│       │               │
│          [ C ]│       │               │
└───────────────┘       └───────────────┘

The spread shares out the free space instead of grouping the children at one spot: spread puts it only between them, even puts it around them too.

 arrange: spread         arrange: even
┌───────────────┐       ┌───────────────┐
│     [ A ]     │       │               │
│               │       │     [ A ]     │
│               │       │               │
│     [ B ]     │       │     [ B ]     │
│               │       │               │
│               │       │     [ C ]     │
│     [ C ]     │       │               │
└───────────────┘       └───────────────┘

Try every combination here:

main home:
  arrange: row, bottom right
  space_inside: 20
  space_between: 8
  color: indigo[50]
  children:
    a:
      size: 20
      color: indigo[300]
    b:
      size: 20
      color: indigo[500]
    c:
      size: 20
      color: indigo[700]

place: is for one container that should float above the others, pinned to a spot of its parent: place: bottom right for a round "+" button, or place: x, y to put its middle at a point out of 100 (place: 50, 10 is centred, near the top). Games move things this way.

main home:
  color: white
  children:
    message:
      text: "A list would go here"
      space_inside: 24
    add_button:
      place: bottom right
      space_around: 24
      size: 16
      shape: circle
      color: blue[600]
      button: "+"
      text_style: 28, white

Text

text: shows text. {name} puts a value inside it; {{ writes a real brace. text_style: holds the size, the weight, the colour and the font, in any order:

first_name = "Ada"

main home:
  arrange: center
  space_between: 8
  children:
    hello:
      text: "Hello, {first_name}!"
      text_style: 32, bold, purple[700]
    quiet:
      text: "Weights: thin, light, bold, heavy; also italic and underline."
      text_style: 14, italic, gray[600]
    serif:
      text: "A font by name, in quotes"
      text_style: 18, "Georgia"

Colour, shape and images

color: is the background. Colours have names (red, teal, gray…), shades from 50 (light) to 900 (dark) like blue[600], or rgb(r, g, b) with an optional fourth number from 0 (see-through) to 1 (solid). A hex colour goes in quotes: "#FF6B6B".

main swatches:
  arrange: row, center
  space_between: 10
  children:
    named:
      size: 20
      color: coral
      shape: circle
    shade:
      size: 20
      color: teal[700]
      shape: rounded
    mixed:
      size: 20
      color: rgb(255, 120, 0, 0.6)
      shadow: true
    faded:
      size: 20
      color: "#6B4EFF"
      see_through: 50
      outline: 2, purple[900]
Attribute Does
shape: square (default), rounded or circle
shadow: true a soft drop shadow
see_through: 50 50 % see-through (0 is solid, 100 invisible)
outline: 2, red a 2-point line round the edge; name sides for some: outline: bottom 1, gray
visible: false hides it (and the space it took)
image: @photo.jpg a picture behind the children; add fill to cover the box, blurred to blur it

@name means a file in the project's assets folder: @cat.png, @backgrounds/sky.jpg. Typing @ lists the files. Web pictures go in quotes: image: "https://…/photo.jpg".

main card:
  size: max, 40
  image: @hero.png, fill
  arrange: bottom left
  space_inside: 16
  children:
    caption:
      text: "Pictures come from the assets folder"
      text_style: 16, bold, white

Buttons, inputs and other kinds

The attribute that names a kind of container also holds its main value:

Write You get
button: "Save" a button with that label
input: "Your name" a text field; the words show while it is empty. Read what was typed as name.text
search: "Search songs" a search field
icon: heart an icon; its size and colour come from text_style
link: "https://…" opens the page when tapped
arrange: column, scroll a list that scrolls
greeting = "Type your name and press Enter"

main form:
  space_inside: 24
  space_between: 12
  children:
    name_box:
      input: "Your name"
      on_change: greeting = "Hello, {name_box.text}…"
      on_submit: greeting = "Hello, {name_box.text}!"
    message:
      text: greeting
      text_style: 18
    row:
      arrange: row, center
      space_between: 8
      children:
        star:
          icon: star
          text_style: 28, amber[500]
        more:
          link: "https://example.com"
          text: "Read more"

Events

An event runs an action when something happens to a container. A short action fits on the line; several actions go one per line underneath.

Event Runs when
on_tap it is tapped
on_hold it is pressed and held
on_touch a finger lands on it (at once, with no tap highlight: for games)
on_swipe a swipe: left:, right:, up:, down: underneath
on_change the text of an input changes
on_submit Enter / Done is pressed in an input
on_hit two containers start to overlap: a, b: action underneath (games)
taps = 0
last = "nothing yet"

main pad:
  arrange: center
  space_between: 12
  color: green[50]
  on_swipe:
    left: last = "swiped left"
    right: last = "swiped right"
  children:
    target:
      size: 40
      shape: circle
      color: green[700]
      arrange: center
      text: "{taps}"
      text_style: 36, bold, white
      on_tap: taps = taps + 1
      on_hold:
        taps = 0
        last = "held: back to zero"
    log:
      text: "Last: {last}"

Data: lists and objects

A variable holds a value: a number, text in quotes, true / false, a list […] or an object { … }.

score = 0
player = "Ada"
playing = true
scores = [12, 40, 7]
user = { name: "Sam", age: 31 }

main show_data():
  scores.add(25)
  print(player, "has", scores.count, "scores")
  print("first", scores.first, "last", scores.last)
  print("has 40?", scores.has(40))
  user.age = user.age + 1
  print(user.name, "is now", user.age)
  scores.remove(7)
  print(scores)
  scores.clear()
  print("left:", scores.count)
On a list Does
items.add(x) adds x at the end
items.remove(x) removes x
items.clear() removes everything
items.count how many there are
items.first, items.last the first and the last
items.has(x) true when x is in the list
items.index(x) where x is (from 0)
items[0] the item at a place, from 0

Objects group named values: user.name reads one, user.age = 32 changes it. A list of objects is the usual way to hold records: tasks = [{ name: "Milk", done: false }].

Functions

A function has brackets after its name and holds statements. It can take values and give one back with return.

add(a, b):
  return a + b

describe(n):
  if (n > 10):
    return "big"
  return "small"

main try_functions():
  total = add(3, 4)
  print("3 + 4 =", total, "which is", describe(total))
  helper(x):
    return x * 2
  print("doubled:", helper(total))

Built-in functions:

Function Does
print(a, b, …) writes a line to the Output panel (or the console screen)
wait(seconds) pauses this function, then carries on
random(1, 7) a whole number from 1 up to 6
go_to(screen), go_back() changes screen (lesson Screens)
every(seconds, action), stop(timer) repeats an action (lesson Timers and games)
play(@sound.mp3), stop(sounds) plays sounds (lesson Sound and video)
visit("https://…") opens a web page

Conditions and loops

if runs a block when its condition is true; else if and else cover the other cases. Combine conditions with and, or and not. The short form a if (condition) else b picks a value.

main grades():
  for score in [95, 82, 64]:
    if (score >= 90):
      print(score, "is an A")
    else if (score >= 80):
      print(score, "is a B")
    else:
      print(score, "needs work")
  n = 0
  while (n < 3):
    n = n + 1
  print("counted to", n)
  late = false
  print("on time" if (not late) else "late")

The short form works in attributes too: color: green if (done) else gray.

Programs without a screen

Mark a function main instead of a container, and Vi runs it once as a console program: no screen, just what it prints. Studio shows a Console screen with the output and a Run again button.

main times_table():
  for n in [1, 2, 3, 4, 5]:
    print(n, "× 7 =", n * 7)

A file can have both a main container and a main function: the app shows the screen and runs the function once when it starts.

Screens

The main container is the first screen. Every other screen is a container you open with go_to(name); go_back() returns. Screens fill the phone and stay clear of the notch and the keyboard by themselves.

tasks = [{ name: "Buy milk", note: "Two litres" }, { name: "Call Sam", note: "About Friday" }]

main home:
  space_inside: 24
  space_between: 12
  children:
    first:
      button: tasks[0].name
      on_tap: go_to(details(tasks[0]))
    second:
      button: tasks[1].name
      on_tap: go_to(details(tasks[1]), fade)
    settings_button:
      button: "Settings"
      on_tap: go_to(settings, slide)

details(task):
  space_inside: 24
  space_between: 12
  children:
    title:
      text: task.name
      text_style: 28, bold
    note:
      text: task.note
    back:
      button: "Back"
      on_tap: go_back()

settings:
  arrange: center
  children:
    label:
      text: "Settings"
    back:
      button: "Back"
      on_tap: go_back()

Containers that take values

A container can take values, like a function: write the names in brackets after its name, then use them in its attributes. Show it with values: task_card(tasks[0]).

tasks = [{ name: "Buy milk", done: true }, { name: "Call Sam", done: false }]

main home:
  space_inside: 20
  space_between: 10
  children:
    task_card(tasks[0])
    task_card(tasks[1])

task_card(task):
  size: max, auto
  space_inside: 14
  shape: rounded
  color: green[100] if (task.done) else white
  shadow: true
  text: task.name
  text_style: strikethrough if (task.done) else normal
  on_tap: task.done = not task.done

Containers written inside it see the values too, however deep:

main home:
  children:
    tab("🧳", "Trips")
    tab("🗺️", "Map")

tab(emoji, label):
  arrange: row
  space_between: 6
  children:
    tab_icon:
      text: emoji
    tab_name:
      text: label
      text_style: 12, bold

Repeating things

repeat: makes copies of a container.

main home:
  children:
    tiles

tiles:
  size: max, 30
  arrange: row, spread
  space_inside: 10
  children:
    tile

tile:
  repeat: 3
  size: 20
  color: yellow
  shape: rounded
tasks = ["Buy milk", "Call Sam", "Water the plants"]

main home:
  space_inside: 20
  space_between: 8
  children:
    task_row
    add:
      button: "Add a task"
      on_tap: tasks.add("Task {tasks.count + 1}")

task_row:
  repeat: task in tasks
  size: max, auto
  space_inside: 12
  color: gray[100]
  shape: rounded
  text: "{index + 1}. {task}"
  on_hold: tasks.remove(task)
main board:
  arrange: center
  children:
    grid:
      size: max, auto
      arrange: row, center
      children:
        cell

cell:
  repeat: 3, 3
  size: 30
  space_around: 2
  shape: rounded
  color: blue[200] if (x == y) else blue[400]
  arrange: center
  text: "{x}, {y}"
  on_tap: cell.at(x, y).color = orange[400]

Reach one copy with .at(): cell.at(1, 1) in a grid, task_row.at(4) in a list.

Reaching other containers

Code reaches a container by its name: title.text, score_label.color.

main home:
  space_inside: 24
  space_between: 12
  children:
    form
    preview

form:
  space_between: 8
  children:
    name_box:
      input: "Type a name"
    save:
      button: "Show it"
      on_tap: preview.text = "Saved: {name_box.text}"

preview:
  text: "Nothing saved yet"
  text_style: 18, gray[700]
  on_tap: preview.text = "Still typing: {form.name_box.text}"

Inside form, save reaches its sibling as name_box. preview is outside form, so it writes form.name_box.

Changing the screen

There are two ways to change a container from code:

main home:
  arrange: center
  space_between: 16
  children:
    light
    switch

light:
  size: 40
  shape: circle
  color: gray[300]
  animate: 0.4 seconds

switch:
  button: "Turn on for 2 seconds"
  on_tap: blink()

blink():
  light:
    color: amber[400]
    shadow: true
  switch.text = "On…"
  wait(2 seconds)
  light:
    color: gray[300]
    shadow: false
  switch.text = "Turn on for 2 seconds"

Timers and games

every(seconds, action) runs an action again and again and gives back a timer; stop(timer) ends it. A game is a step function that every runs about 30 times a second.

ball_y = 10
speed = 0
loop = 0

step():
  speed = speed + 0.3
  ball_y = ball_y + speed
  if (ball_y > 90):
    ball_y = 90
    speed = 0 - speed * 0.7

drop():
  stop(loop)
  ball_y = 10
  speed = 0
  loop = every(0.03 seconds, step())

main court:
  color: slate[900]
  on_touch: drop()
  children:
    ball:
      place: 50, ball_y
      size: 10
      shape: circle
      color: orange[400]
    hint:
      place: 50, 96
      text: "Tap to drop the ball"
      text_style: 14, white
main field:
  on_hit:
    ball, wall: bounce()
    ball, goal:
      score()
      play(@cheer.mp3)

The Comet Glide template is a whole game built this way.

Sound and video

main player:
  arrange: center
  space_between: 12
  children:
    jingle:
      button: "Play a sound"
      on_tap: play(@ding.mp3)
    music:
      button: "Music on (loops)"
      on_tap: play(@music.mp3, loop)
    quiet:
      button: "Stop all sounds"
      on_tap: stop(sounds)
    clip:
      size: max, auto
      video: @intro.mp4
      loop: true
      volume: 80
    controls:
      arrange: row
      space_between: 8
      children:
        go:
          button: "Play"
          on_tap: play(clip)
        hold:
          button: "Pause"
          on_tap: pause(clip)
        back:
          button: "Stop"
          on_tap: stop(clip)

Keeping data

saved best = 0
saved names = []
secret token = ""

main game:
  children:
    score:
      text: "Best: {best}"
    again:
      button: "Clear"
      on_tap: reset(best)

The internet and the cloud

weather = empty

get_weather():
  weather = load("https://api.example.com/weather.json", 10 seconds)

main home:
  on_open: get_weather()
  children:
    sky:
      text: "{weather.sky}" if weather != empty else "No answer yet"

The cloud (Supabase). Put the project's address and its public key in config:; then cloud tasks = [] is the tasks table, kept live both ways (cloud tasks from todo_items = [] for another table name). sign_in(email, password), sign_up(…), sign_out() and current_user() handle accounts; upload(photo) stores a file and gives back its web address. Only the public key belongs in an app: Vi refuses a secret key.

The phone

photo = empty

snap():
  photo = take_photo()
  vibrate()

main home:
  arrange: center
  space_between: 12
  children:
    picture:
      size: 60, 60
      image: photo, fill
    take:
      button: "Take a photo"
      on_tap: snap()
    tell:
      button: "Share"
      on_tap: share(photo)

Games

main game:
  size: max, max
  on_key:
    space: jump(player)
    p: pause()
  children:
    player:
      size: 10, 10
      color: orange
      move: arrows
      gravity: normal
      stay_inside: parent
    ground:
      size: max, 5
      place: bottom
      color: green
      solid: true

Popups and menus

main inbox:
  on_back: sure.visible = false
  children:
    ask:
      button: "Delete"
      on_tap: sure.visible = true
    sure:
      visible: false
      layer: top
      size: max, max
      color: black
      see_through: 50
      appear: fade
      on_tap: sure.visible = false
      children:
        card:
          size: 80, auto
          color: white
          shape: rounded
          space_inside: 20
          text: "Delete this message?"

Pages and tabs

main home:
  children:
    pages:
      arrange: pages
      size: max, max
      children:
        news
        settings
    tabs:
      arrange: row, spread
      children:
        to_news:
          icon: home
          spoken: "News"
          color: blue if (pages.page == news) else gray
          on_tap: go_to(news)
        to_settings:
          icon: settings
          spoken: "Settings"
          color: blue if (pages.page == settings) else gray
          on_tap: go_to(settings, fade)

news:
  text: "News"

settings:
  text: "Settings"

Dates and time

party = date(2026, 12, 25)

main home:
  children:
    clock:
      text: "{now().weekday}, {now().time}"
    left:
      text: "{time_until(party).days} days to go"

Text and number helpers

main sums():
  name = "  ada lovelace "
  print(name.tidy.upper)
  print("a,b,c".split(","))
  print(["x", "y"].join(" and "))
  print(3.14159.round(2))
  print(1250000.commas)
  print([4, 8, 6].average)
  print(number("42") + 1)

Forms

main signup:
  space_between: 12
  space_inside: 20
  children:
    email_box:
      input: "Email"
      expects: email
      on_submit: focus(password_box)
    password_box:
      input: "Password"
      expects: password
    bio:
      input: "About you"
      expects: long_text
      limit: 200
    loud:
      slider: 0 to 100
      on_change: print(loud.value)

Maps

main where:
  children:
    town:
      map: "Lagos"
      zoom: city
      size: max, 60
      children:
        shop:
          place: location(6.60, 3.35)
          icon: location

Purchases and ads

Themes and dark mode

themes:
  light:
    paper: white
    ink: gray[900]
  dark:
    paper: gray[900]
    ink: white

main home:
  color: paper
  on_open: use_theme(dark if dark_mode() else light)
  on_dark_mode: use_theme(dark if dark_mode() else light)
  children:
    hello:
      text: "Hello"
      text_style: ink

Accessibility

More than one language

config:
  languages: english, yoruba

main home:
  on_open: use_language(phone_language())
  children:
    hi:
      text: { english: "Hello", yoruba: "Ẹ n lẹ" }

JavaScript, for what Vi doesn't cover

src/js/party.ts
  export function celebrate() { … }
  export function sales_chart({ data }) { return <BarChart data={data} />; }

src/main.vi
  config:
    packages: "react-native-gifted-charts"
  win():
    celebrate()
  report:
    children:
      sales_chart(monthly_sales)

Files and projects

A small example fits in one file. A real app reads better when each file does one job. Every new project is laid out like this:

my-app/
  README.md
  assets/              icon.png, splash.png, your pictures and sounds
  src/
    main.vi            the main screen: puts the pieces together
    config.vi          app name, icon, splash screen, store id
    theme.vi           colours used everywhere
    tasks.vi           the data, and the functions that change it
    components/
      header.vi        one piece of the screen per file
      task_row.vi

The config block

The app's settings live in one config: block, in src/config.vi.

config:
  name: "Tasks"
  id: com.yourname.tasks
  version: "1.2.0"
  icon: @icon.png
  splash:
    color: white
    logo: @splash.png
    text: "Tasks"
    time: 1 second

main home:
  arrange: center
  children:
    title:
      text: "Welcome to {config.name}"
Key Means
name the app's name under its icon
id the store id, like a reversed web address. Never change it after publishing, or the stores see a different app
version the version people see in the store, like "1.2.0"; raise it for each release
icon the launcher icon (a square picture, 1024 × 1024 is best)
splash the screen while the app starts: color, logo or image, text, seconds
screens changes the sizes where phone, tablet and desktop start (rarely needed)

config.name (and the other keys) can be read anywhere.

Errors and good habits

Vi checks the code as you type. Problems show as red (errors: the app can't run) or yellow (warnings: it runs, but something is off) underlines and in the Problems tab. Most have a Fix button there, or a light bulb in the editor.

You see It means The fix
Unknown container 'heder' a misspelt or missing name pick the suggested name, or Create container 'heder'
Unknown function 'ad_task' the function doesn't exist pick the suggested name, or Create function 'ad_task()'
Unknown attribute 'colour' not an attribute Vi knows Change to 'color'
Nothing to run: mark the screen with 'main'… no main container Add main container, or Mark … as the main container
Your own files need no import… an import of a file of the project Remove this import line
'header' is also defined in src/a.vi the same name in two files rename one of them
'name_box' is defined inside 'form' and is not visible here an inside name used from outside Use 'form.name_box'

Good habits:

Every example here runs in Vi Studio: open it in your browser (phones too) and press Start learning on the welcome page. Each code block there has a Try it button that runs it in the phone preview.