# Editing from the command line

> Read and change a project with `vidmake get`, `set`, `unset`, `add` and `rm`, addressing values by layer id, composition id or JSON pointer. Every edit is compiled before it is saved.

A vidmake project is a JSON file, so you can always edit it directly. For small changes, the edit commands are
often quicker and safer, especially for agents: you address a value by layer id instead of by line number, and
**every edit is compiled before it's saved**, so a typo can't break a project that worked.

```bash
vidmake get   project.json title.fontSize            # read a value
vidmake set   project.json title.fontSize 96         # write a value
vidmake unset project.json title.enter               # delete a field (or a whole layer)
vidmake add   project.json '{"id": "logo", "type": "image", "src": "logo.svg"}'
vidmake rm    project.json logo                      # delete a layer by id
```

The transcripts on this page come from a copy of the [inspecting demo](https://vidmake.app/docs/inspecting.md) (composition `card`
with layers `panel`, `title`, `ring` and `label`).

## Paths

`get`, `set` and `unset` take a path in one of three forms:

| form | example | resolves to |
|---|---|---|
| `<layerId>.<field>[.<sub>...]` | `title.fontSize`, `ring.endAngle.to` | a field of the layer with that id, at any depth (inside groups too) |
| `<compId>.<field>[...]` | `card.duration`, `card.props.title` | a field of the composition with that id |
| JSON pointer | `/compositions/0/fps`, `/fonts/0/path` | exactly that location ([RFC 6901](https://www.rfc-editor.org/rfc/rfc6901)) |

- The first segment is looked up as a layer id first, then as a composition id. Layer search is depth-first in file
  order, through `children` of groups, across all compositions.
- After the id, each `.segment` is an object key or an array index: `title.text.0.fill` is the `fill` of the first
  span of a [rich text](https://vidmake.app/docs/text.md) layer, `bar.repeat.count` the count of a repeat.
- A path that starts with `/` is a JSON pointer and is used as is. Use it for anything without an id: fonts,
  layers without an `id`, or keys containing a dot. The commands print the pointer they resolved, so you can
  reuse it.
- The bare layer id (no field) addresses the whole layer: `get project.json title` prints it, `unset project.json title` deletes it.

> [!WARNING]
> Ids are not required to be unique. If two layers share an id, paths and `rm` always pick the first one in the
> file. Give every layer a distinct `id`; the copies made by [`repeat`](https://vidmake.app/docs/repeat.md) (`bar.0`, `bar.1`) exist
> only at render time and can't be addressed: edit the original layer.

## get

`get` prints the value at a path as pretty JSON.

```bash
vidmake get project.json title.fontSize
vidmake get project.json ring.endAngle
vidmake get project.json /compositions/0/fps
```

```output
72
{
  "from": 0,
  "to": 360,
  "duration": "2s",
  "ease": "easeInOutCubic"
}
30
```

It reads the file as written, before props and defaults are applied: a field you didn't set is reported as
missing, not as its default (``error: `title.shadow` (/compositions/0/layers/1/shadow) not found``). For resolved
values at a given time, use [`inspect`](https://vidmake.app/docs/inspecting.md#inspect).

`get` works on the source file, so it's safe in scripts and in this documentation:

```console
$ vidmake get inspect-scene.json title
{
  "id": "title",
  "type": "text",
  "text": [
    "Every frame, ",
    {
      "text": "checkable",
      "italic": true,
      "fontWeight": 400
    }
  ],
  "font": "Schibsted Grotesk",
  "fontWeight": 860,
  "fontSize": 64,
  "letterSpacing": -2.8,
  "fill": "#161412",
  "y": "40%",
  "start": "0.6s",
  "enter": "pop",
  "exit": "fade"
}
```

## set

`set <project> <path> <value>` writes one value. The value is parsed as JSON when it is valid JSON, and otherwise
taken as a plain string, so quotes are optional for most strings:

```bash
vidmake set project.json title.text "Checked before render"     # not valid JSON -> the string
vidmake set project.json title.fontSize 64                       # a number
vidmake set project.json card.duration 5s                        # a string
vidmake set project.json title.enter '{"type": "fadeUp", "duration": "0.6s"}'
vidmake set project.json /compositions/0/background '"#f4f1ea"'
vidmake set project.json panel.shadow.blur 30                    # creates "shadow": {"blur": 30}
```

```output
set /compositions/0/layers/1/text
set /compositions/0/layers/1/fontSize
set /compositions/0/layers/1/enter
set /compositions/0/duration
set /compositions/0/background
set /compositions/0/layers/0/shadow/blur
```

- Missing objects along the path are created (`panel.shadow.blur` above).
- An array index one past the end, or `-` in a JSON pointer, appends: `set project.json /compositions/0/layers/- '{...}'`.
- Strings that look like JSON are parsed as JSON. `set title.text 42` writes the **number** 42, which a text layer
  rejects; write `'"42"'` (shell quotes around JSON quotes) to get the string. The same goes for `true`, `false`,
  `null` and anything starting with `[` or `{`.
- The whole file is rewritten as pretty-printed JSON with 2-space indentation. Key order is kept, but compact
  one-line layers are expanded. A file holding a single bare composition is saved in the full
  `{"compositions": [...]}` form.

### Compiled before saving

Before writing, `set` (and `add`) compile **every composition** in the file with its default props, exactly as
`validate` would. If anything fails, nothing is written and the command exits with status 1:

```bash
vidmake set project.json title.fontsize 64
```

```output
error: edit not saved, the project would be invalid:
  composition `card`: unknown field `fontsize` — did you mean `fontSize`?
(use --force to save anyway)
```

```bash
vidmake set project.json ring.endAngle '{"from": 0, "to": 360, "duraton": "2s"}'
```

```output
error: edit not saved, the project would be invalid:
  layer `ring` (card > layers[2]): `endAngle`: tween needs `end` or `duration` (or `spring`): {"from":0,"to":360,"duraton":"2s"}
(use --force to save anyway)
```

`--force` saves anyway and prints the problems as warnings on stderr. Use it when you are mid-way through a
change that takes several edits; `validate` will keep reporting the error until it is fixed.

```output
warning: layer `ring` (card > layers[2]): `endAngle`: tween needs `end` or `duration` (or `spring`): {"from":0,"to":360,"duraton":"2s"}
set /compositions/0/layers/2/endAngle
```

> [!IMPORTANT]
> The check covers the whole project, not just your edit. If the file is already invalid, every `set` and `add`
> is refused (the error lists the existing problem) until you fix it, or pass `--force`. Lint warnings
> (`offCanvas` and the like) never block an edit; run `vidmake validate` to see them.

## unset

`unset <project> <path>` deletes a field, an array item, or a whole layer (when the path is a bare layer id).

```bash
vidmake unset project.json panel.shadow
vidmake unset project.json title.exit
```

```output
removed /compositions/0/layers/0/shadow
removed /compositions/0/layers/1/exit
```

Deleting can make a project invalid (removing a required field, or a layer another layer `attach`es to). Unlike
`set`, `unset` **always saves** and prints what is now wrong as a warning, so follow it with `validate`:

```output
warning: layer `title` (card > layers[2]): text layer needs `text`
removed /compositions/0/layers/2/text
```

## add

`add <project> <layer>` inserts one layer (a JSON object) or several (a JSON array). By default it appends them to
the first composition's `layers`, which puts them on top of everything else.

```bash
vidmake add project.json '{"id": "badge", "type": "text", "text": "NEW", "font": "sans-serif", "fontSize": 20, "fill": "#c92a26", "y": "75%", "start": "1.5s", "enter": "fadeUp"}'
vidmake add project.json '{"id": "bg", "type": "rect", "fill": "#f4f1ea"}' --index 0
```

```output
added 1 layer(s) at /compositions/0/layers/4
added 1 layer(s) at /compositions/0/layers/0
```

| option | effect |
|---|---|
| `--index <n>` | insert at position n (0 = bottom-most); default is the end (top-most). Larger values append |
| `--parent <groupId>` | insert into that group's `children` (created if missing) instead of the composition |
| `-c, --comp <id>` | insert into that composition's `layers` (ignored with `--parent`) |
| `--force` | save even if the result doesn't compile |

The layer argument can also be `@file.json` (a file path relative to the current directory, not the project) or
`-` to read from stdin, which avoids shell quoting for longer layers:

```json title="dots.json"
[
  { "id": "dot-a", "type": "ellipse", "width": 12, "x": "20%", "fill": "#1f5fb0" },
  { "id": "dot-b", "type": "ellipse", "width": 12, "x": "80%", "fill": "#c92a26" }
]
```

```bash
vidmake add project.json @dots.json
echo '{"id": "stamp", "type": "star", "width": 40, "fill": "#4a4640"}' | vidmake add project.json -
vidmake add project.json '{"id": "group", "type": "group", "children": []}'
vidmake add project.json '{"id": "inner", "type": "rect", "width": 10, "height": 10}' --parent group
```

```output
added 2 layer(s) at /compositions/0/layers/6
added 1 layer(s) at /compositions/0/layers/8
added 1 layer(s) at /compositions/0/layers/9
added 1 layer(s) at /compositions/0/layers/9/children/0
```

Like `set`, `add` compiles the result first. The errors you will meet:

```output
error: edit not saved, the project would be invalid:
  composition `card`: unknown field `widht` — did you mean `width`?
(use --force to save anyway)
error: no composition `nope`
error: no group `nope`
```

`--parent` accepts any layer id; adding children to a layer that isn't a `group` is caught by the compile check
(``field(s) children do not apply to `rect` layers``).

## rm

`rm <project> <layerId>` deletes the first layer with that id, wherever it is, including its children.

```bash
vidmake rm project.json stamp
```

```output
removed layer stamp (/compositions/0/layers/8)
```

A missing id fails with ``error: no layer `stamp` ``. Like `unset`, `rm` always saves and reports any resulting
problem (for example a sibling that `attach`ed to the removed layer) as a warning.

## Exit codes

| result | exit status |
|---|---|
| saved | 0 (warnings, if any, on stderr) |
| refused by the compile check, unknown id or path, invalid JSON argument | 1 |

All messages go to stderr, the success line (`set ...`, `added ...`, `removed ...`) to stdout. See the
[CLI page](https://vidmake.app/docs/cli.md#exit-codes) for the other commands.

## Workflow

> [!AGENT]
> For a small change, `set` is cheaper and safer than rewriting the file: one command, a precise path, and a
> compile check. Then look at the result (`vidmake still project.json -t <time>` or a storyboard) before moving on.
> For larger restructuring (new scenes, reordering many layers) edit the JSON directly and run
> `vidmake validate`.

A typical session:

```bash
vidmake inspect project.json -t 2s -l title          # title is 40px too low
vidmake set project.json title.y '"38%"'
vidmake still project.json -t 2s -o check.png         # look at it
vidmake set project.json title.enter pop
vidmake trace project.json -l title --every 2 --to 1.5s
```

Related: [validate and lint](https://vidmake.app/docs/lint.md), [inspecting](https://vidmake.app/docs/inspecting.md), [the JSON Schema](https://vidmake.app/docs/schema.md) for
editor completion.
