How to Write a Team Rule That Applies Only to Certain Files
Use glob patterns in a .mdc rule so a convention attaches to just the files it governs, like tests or API routes.
Not every rule should apply everywhere. A testing convention has no business loading when you edit a CSS file. Glob patterns let a rule attach only when a matching file is in context, which keeps the model focused and your context window lean. This guide writes a scoped rule for React components.
What you need
- A project with a .cursor/rules folder
- A convention that only applies to a subset of files
- Knowledge of where those files live in the tree
Step 1: Decide the pattern
Glob syntax matches paths. A double star spans directories, a single star matches within one segment. Sketch the pattern that captures your files and nothing else before writing the rule.
| Glob | Matches |
|---|---|
| src/components/**/*.tsx | Every component file under components |
| **/*.test.ts | Test files anywhere in the repo |
| app/api/**/route.ts | Route handlers in the app router |
| **/*.{css,scss} | All stylesheets |
Step 2: Write the rule with the glob
Create the file and set the glob in the frontmatter. Leave alwaysApply false so the rule only loads when a matching file is in context, which is the whole point of scoping.
---
description: React component conventions
globs: ["src/components/**/*.tsx"]
alwaysApply: false
---
- One component per file; the file name matches the component.
- Props are a named `type`, not an inline object.
- No default exports.
- Co-locate styles in a sibling `.module.css` file.Step 3: Verify it attaches and detaches
Open a component file and start a chat: the rule should appear in the attached context. Then open a file outside the glob, like a server utility, and confirm the rule no longer loads.
Step 4: Commit it for the team
Because rules live in the repo, committing the file shares the convention with everyone. New teammates get the same guidance with no setup the moment they clone.
Result: your component conventions are enforced automatically while editing components, and stay out of the way everywhere else, shared across the whole team through version control.
Watch related tutorials
1:42:18
28:14
41:09
9:47
8:23
52:31