CursorIntermediate

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.

6 minIntermediate

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.

GlobMatches
src/components/**/*.tsxEvery component file under components
**/*.test.tsTest files anywhere in the repo
app/api/**/route.tsRoute 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.

.cursor/rules/components.mdc
---
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.

Cursor — attached rules
You
[editing src/components/Button.tsx] Add a loading state.
Agent
Attached rule: components.mdc. Added an isLoading prop typed in ButtonProps, kept the named export.
The component rule loads only for matching files.

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.

zsh — project root
$git add .cursor/rules/components.mdc
$git commit -m "Add component conventions rule"
1 file changed, 12 insertions(+)
$
Test the glob before trusting it
A glob that is too broad quietly loads the rule everywhere and wastes context; too narrow and it never fires. Open one file that should match and one that should not, and confirm the rule appears and disappears as expected.

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

Tags
#rules#globs#conventions#team