# mt link

`mt link` adds edges to a task’s `links:` field. You name each target by its stem or its stem hash, and `mt` writes the path to its file for you. It’s the only command that writes `links:`. [Task links](https://mlavrinenko.github.io/mindtape/links/index.md) describes the edge kinds, how `mt` checks a target, and the map `--apply` reads.

Link tasks: one link at a time, or many from a file

`mt link <task> <kind> <target>...` adds a link to each target, and `mt link --apply <file>` adds every link a YAML or JSON file lists. Running either twice doesn't add anything the second time.

`mt link <task> <kind> <target>...` adds one edge per target. `mt link --apply <file>`, or `-` for stdin, reads a YAML or JSON map of [stem](https://mlavrinenko.github.io/mindtape/concepts/index.md#identity) to kind to targets, shown on [Task links](https://mlavrinenko.github.io/mindtape/links/index.md#the-apply-map). Both forms merge. An edge with the same kind and target isn’t added twice, so running either again writes nothing new. With `--apply`, `--replace` uses the map as the edge set of each stem in the map. `mt` checks the kind and the target before it writes. `mt` refuses a kind that requires a note, such as the built-in `related`, when the edge has none. When one entry fails, `mt` reports it and writes the rest. `--dry-run` prints the plan instead.

Runs on one project and refuses a [set](https://mlavrinenko.github.io/mindtape/read-many-projects/index.md).

- `[TASK]`: The task: its stem, its hash, or the path to its file
- `[KIND]`: The kind of link, such as `depends-on` or `related`
  The built-in kinds are `depends-on`, `parent`, `recurred-from`, `supersedes`, `duplicates` and `related`, and your project can add more.
- `[TARGETS]…`: The tasks to link to, each by stem, hash or path
  A path is relative to the task. `mt link` refuses a stem that tasks in two folders share, so give a path then.
  `repeatable`
- `-m, --note <NOTE>`: A note on the link
  A `related` link needs one, unless your project changes that. With `--apply`, put notes in the file instead.
- `--apply <FILE>`: Read many links from a YAML or JSON file, or `-` for stdin
  Use it instead of naming one task. The file maps each task's stem to kinds, and each kind to targets.
- `--replace`: With `--apply`, remove the links the file leaves out
  The file becomes the full set of links for each task it names. Without `--replace`, mt only adds.
- `--dry-run`: Show what would change without writing anything

```
$ mt link write-docs depends-on wire-oauth
linked write-docs: +depends-on -> wire-oauth.typ
$ mt link drop-legacy-cli depends-on auth-epic write-docs
linked drop-legacy-cli: +depends-on -> auth-epic.typ, +depends-on -> write-docs.typ
$ mt link write-docs related search-too-slow -m "same release"
linked write-docs: +related -> search-too-slow.typ
$ mt link --apply links.yaml --replace --dry-run
would rewrite 1 task:
  - search-too-slow: +depends-on -> auth-epic.typ
dry run: nothing written
$ mt link --apply links.yaml
linked search-too-slow: +depends-on -> auth-epic.typ
$ mt link --apply - < links.yaml
unchanged search-too-slow: every edge was already there
```

Every command also takes the [global flags](https://mlavrinenko.github.io/mindtape/cli/index.md#global-flags).
