Gmail filters work fine until they don’t. You set up a rule to label court notices, another to archive newsletters, and a third to keep vendor invoices out of the way. A year later nobody remembers what each rule does, and the one you added last spring is quietly archiving replies from your own colleagues.
I wanted the firm’s filters in a file I could read, diff and version. The tool I landed on is gmailctl, a command-line program that reads a config file and pushes the result to Gmail through Google’s API.
Why bother
Your inbox runs on rules, or lack thereof. Court notices go to a folder, bar mail gets its own label, and a few senders skip the inbox entirely. Those rules decide what you see first, so they deserve the same care as anything else in the practice.
Clicking through Gmail’s settings works, but it leaves no history. When a rule breaks, you can’t ask what changed last week. With a config file in a git repository, you can.
How it’s set up
The config is written in Jsonnet, a layer on top of JSON that lets you define helpers once and reuse them. I use two. One archives a message and applies a label. The other applies a label and keeps the message in the inbox. That lets a rule like “court opinions” say what it does in one line, and it keeps the config readable for the next person who opens it. (That next person may be me in six months.)
The config lives in a local folder that is its own git repository. Two files sit beside it, a client credential and a sign-in token. Both are secrets. Keep them out of the repository, and out of any notes you keep.
Setup on the Google side takes a few steps:
- Create a project in your Google Cloud organization and enable the Gmail API.
- Create a desktop-app OAuth client for the sign-in.
- Set the consent screen to Internal. That setting is available only to Workspace accounts, and it means the sign-in token does not expire every seven days.
- Sign in once from an interactive terminal. The tool prints a Google sign-in URL and starts a temporary local server to receive the token.
I was glad to see what the tool asks for. It requests permission to manage labels and basic settings. It does not request permission to read mail. That’s the right shape for a tool that only manages filters. If a utility asks to read your whole mailbox to change a filter, ask why.
Daily use
The loop is short:
cd ~/.config/gmailctl
gmailctl diff # preview what would change in Gmail
gmailctl apply # push the change (asks first; --yes skips the prompt)
git commit -am "Route bar notices to their own label"
gmailctl diff is the habit that matters. It shows what would change before anything touches your account. I ran init, download, diff, and apply --yes on a homelab machine, and all four worked.
Drift is the other thing to watch. If someone edits a filter in Gmail’s web interface, the config and the live rules no longer match. Run gmailctl diff to see the difference. Then either bring the config into line by hand or run gmailctl download, which pulls Gmail’s version down. Be careful with download. It overwrites the config’s structure, so commit your work first.
Labels are outside this system. My config doesn’t manage them, so I still create, rename and remove labels in Gmail itself. That’s a gap, and I’d rather name it than pretend it’s handled.
Lessons from the first audit
Building the config taught me more about filter design than the tool did. These are the four that cost me the most time.
Gmail runs every matching filter. A narrow rule can add a label or a star, but it can’t undo another rule’s archive. If a broad rule archives a category of mail, the exceptions have to be excluded from that rule explicitly. I wrote the exclusion into the broad rule itself, so the court-notice filter skips anything from the court’s amendment list and the internal notice address. Those stay in the inbox.
Catch-all rules on body words catch real correspondence. My catch-all for newsletters matched the word “unsubscribe.” That word shows up in the footers of list messages, and also in quoted text from replies between people who are simply discussing a list. Colleagues’ replies got archived. The fix was to exclude Gmail’s Primary category, so a message a person wrote to me never falls through to the newsletter rule.
Partial addresses don’t match in -from: exclusions. I wrote an exclusion for a sender by its first half, and it failed to match the full domain. Use full addresses or full domains. I also moved the sender lists for the firm’s payment and IT notices into shared variables, so the two rules that use them can’t drift apart.
Filters don’t touch old mail. A fixed rule only acts on messages that arrive after the fix. Old messages keep whatever label or archive state the broken rule gave them. To clean up, search by label and category, check what’s there, and correct it by hand. A search like label:feed-other category:primary finds the leftovers.