# Rules

A rule matches links and sends them to an intent. Rules are tried top to bottom, and the first match wins.

## A rule

```toml
[[rules]]
name = "Linear"
host = "*.linear.app"
intent = "work"
```

Every rule needs an `intent` and at least one matcher. `name` is optional and shows up in traces:

<pre class="trace">Slack → rule 2 "Linear" → @work → Chrome · Work ▸</pre>

## Matchers

| Field | Example | Matches |
| --- | --- | --- |
| `host` | `"*.linear.app"` | `linear.app` and every subdomain. Without `*.`, that exact host only. |
| `prefix` | `"https://docs.google.com/document/"` | Links that start with this text. |
| `path` | `"/acme/"` | Links whose path starts with this. |
| `from` | `"com.tinyspeck.slackmacgap"` | Links clicked in this app (its bundle ID). |
| `regex` | `"^https://github\\.com/acme/.*"` | Links the pattern matches **in full**. |
| `modifier` | `"option"` | Links clicked while this key is held. |

Matching happens on the [cleaned](/cleaning) link, so a rule never has to account for tracking parameters.

### Combining matchers

Fill in several fields and **all** of them must match. Give one field a list and **any** item may match.

```toml
[[rules]]
name = "Work repos from chat"
host = ["github.com", "gitlab.com"]   # either host
path = "/acme/"                        # and this path
from = "com.tinyspeck.slackmacgap"     # and clicked in Slack
intent = "work"
```

### Hosts are matched properly

Ferry compares the link's real host, never a piece of text inside the link.

| Link | `host = "*.linear.app"` |
| --- | --- |
| `https://linear.app/acme` | Matches |
| `https://app.linear.app/acme` | Matches |
| `https://LINEAR.app./acme` | Matches. Case and a trailing dot don't matter. |
| `https://notlinear.app/x` | No match |
| `https://linear.app@evil.example/x` | No match. The host is `evil.example`. |

### Regex means the whole link

A `regex` must match the entire link, not just part of it. End a prefix-style pattern with `.*`:

```toml
regex = "^https://github\\.com/acme/.*"
```

For a plain "starts with", `prefix` is simpler and can't be got wrong.

## Order is yours

Rules are tried in the order they appear. The first enabled rule that matches decides; Ferry never re-sorts by how specific a rule looks.

Put narrow rules above broad ones:

```toml
[[rules]]
name = "Zoom calls"
host = "*.zoom.us"
path = "/j/"
intent = "meeting"

[[rules]]
name = "Everything else on Zoom"
host = "*.zoom.us"
intent = "work"
```

In **Settings › Rules**, the arrows move a rule up or down.

## When nothing matches

The link goes to the `fallback` intent. It starts as `ask`, which opens the picker.

```toml
fallback = "ask"
```

<pre class="trace">Mail → no rule matched (0 of 2) → ask</pre>

Set it to another intent to send unmatched links straight there. You can also switch it from the menu bar.

## Turning rules off

```toml
[[rules]]
name = "Linear"
host = "*.linear.app"
intent = "work"
enabled = false    # skipped, but kept
clean = false      # this rule's links keep their tracking parameters
```

## Rules that can't run

A rule that names an intent that doesn't exist is skipped, and the trace says so instead of hiding it:

<pre class="trace">Slack → ! rule 1 invalid: unknown intent @wrok → rule 2 "Linear" → @work → Chrome · Work ▸</pre>

Settings marks the rule and shows a banner. A misspelled field name, like `hsot`, is an error when the file loads, so it can't silently create a rule that never matches.

## Faster ways to add rules

- **In the picker**, <kbd>⌘</kbd> <kbd>↩</kbd> opens the link and saves a rule for its site.
- **In Settings › Rules**, sites you routed by hand appear under **Suggested**.
- **Test a link** offers to add a rule when nothing caught the link you pasted.
- **An AI agent** can [propose a rule](/agents#proposals), which you approve or reject.
