Custom Rules and Templates
Your team has five developers using Cursor. One of them always gets clean, well-structured output. The others get inconsistent results — different naming conventions, different error handling patterns, different file organization. The difference is not prompting skill. The developer with consistent results has a .cursor/rules/ directory with 12 carefully crafted rule files that teach the agent how your team writes code.
Rules are the single most impactful investment you can make in Cursor productivity. They persist across every chat session, apply automatically based on file patterns, and ensure every developer on your team gets the same quality of AI output.
What You’ll Walk Away With
Section titled “What You’ll Walk Away With”- A framework for deciding what to encode as rules versus what to leave as ad-hoc prompts
- Rule file structure with frontmatter for glob patterns, descriptions, and auto-apply settings
- A starter library of production-tested rules you can adapt to your project
- Techniques for organizing, composing, and maintaining rules at scale
Rule Types and When to Use Each
Section titled “Rule Types and When to Use Each”Cursor supports four rule application modes:
| Type | Frontmatter | When Applied | Use For |
|---|---|---|---|
| Always Apply | alwaysApply: true | Every chat session | Core coding standards, file conventions |
| Apply Intelligently | alwaysApply: false + description | Agent decides based on relevance | Domain-specific patterns, workflow guides |
| Glob-Scoped | globs: "src/api/**/*.ts" | When matching files are in context | Directory-specific conventions |
| Manual | No frontmatter or alwaysApply: false without description | When @rule-name is mentioned | Specialized workflows, templates |
The Rule Decision Framework
Section titled “The Rule Decision Framework”Ask yourself: “Does every single prompt benefit from this information?”
- Yes -> Always Apply (file naming, code style, project structure)
- Only when working in a specific area -> Glob-Scoped (backend conventions, frontend patterns)
- Only for certain types of tasks -> Apply Intelligently (migration guide, API creation steps)
- Rarely, but critical when needed -> Manual (security audit checklist, release process)
Building Your Rules Library
Section titled “Building Your Rules Library”Rule 1: Project Overview (Always Apply)
Section titled “Rule 1: Project Overview (Always Apply)”Every project should have one always-apply rule that gives the agent essential context:
Rule 2: Code Style (Always Apply, Glob-Scoped)
Section titled “Rule 2: Code Style (Always Apply, Glob-Scoped)”---globs: "*.ts,*.tsx"alwaysApply: true---
TypeScript conventions for this project:- Use `interface` for object shapes, `type` for unions and intersections- Prefer `async/await` over `.then()` chains- Use `unknown` instead of `any`- Destructure function parameters when there are more than 2- All exported functions must have JSDoc comments- Prefer early returns over nested conditionalsRule 3: Feature Implementation Guide (Agent-Decided)
Section titled “Rule 3: Feature Implementation Guide (Agent-Decided)”---description: Step-by-step guide for implementing a new feature end-to-endalwaysApply: false---
When implementing a new feature:
1. Database: Create migration in src/db/migrations/ using Drizzle2. Types: Add TypeScript types in src/types/3. API: Create route handler in src/app/api/4. Service: Add business logic in src/lib/services/5. UI: Build components in src/components/ and page in src/app/6. Tests: Write tests adjacent to the source files
Always check for existing patterns before creating new ones.Reference @src/app/api/users/route.ts as the canonical API example.Reference @src/components/UserProfile.tsx as the canonical component example.Rule 4: Testing Conventions (Glob-Scoped)
Section titled “Rule 4: Testing Conventions (Glob-Scoped)”---globs: "**/*.test.ts,**/*.test.tsx,**/*.spec.ts"---
Testing conventions:- Use Vitest with happy-dom for DOM tests- Test file lives adjacent to source: foo.ts -> foo.test.ts- Use descriptive test names: "should return 401 when token is expired"- Mock external services, never make real HTTP calls in unit tests- Use factory functions for test data, not inline object literals- Test error paths, not just happy paths
See @src/lib/services/__tests__/user-service.test.ts for a complete example.Referencing Files in Rules
Section titled “Referencing Files in Rules”Rules can reference other files using @filename syntax. This is far better than copying code into the rule because the reference always points to the current version:
---description: Create a new API endpointalwaysApply: false---
Follow the pattern in @src/app/api/users/route.ts exactly:- Same error handling structure- Same response format- Same middleware chain- Same validation approach using Zod schemas from @src/lib/validators/
Do not copy the user-specific logic. Only copy the structural patterns.Team Rules (Team and Enterprise Plans)
Section titled “Team Rules (Team and Enterprise Plans)”Team and Enterprise plans support centralized rules managed from the Cursor dashboard. These rules apply across all projects for every team member.
Team Rules are useful for:
- Organization-wide coding standards
- Security policies
- Compliance requirements
- Shared conventions that span multiple repositories
Precedence Order
Section titled “Precedence Order”When rules conflict, they are applied in this order:
- Team Rules (highest precedence)
- Project Rules (
.cursor/rules/) - User Rules (personal preferences)
This means a team admin can enforce standards that individual developers cannot override.
Importing and Sharing Rules
Section titled “Importing and Sharing Rules”Remote Rules from GitHub
Section titled “Remote Rules from GitHub”Import rules directly from GitHub repositories:
- Open Cursor Settings > Rules, Commands
- Click + Add Rule next to Project Rules
- Select Remote Rule (GitHub)
- Paste the repository URL
Imported rules stay synced with the source repository. Updates to the remote rule are automatically reflected in your project.
Agent Skills Integration
Section titled “Agent Skills Integration”Cursor can load rules from Agent Skills, an open standard for extending AI agents. Enable them in Cursor Settings > Rules > Import Settings > Agent Skills. These are treated as agent-decided rules — Cursor determines when they are relevant based on context.
When This Breaks
Section titled “When This Breaks”Rules are not being applied. Check that the rule file is in .cursor/rules/ with the correct extension (.md or .mdc). For .mdc files, verify the frontmatter syntax. View applied rules by hovering the context gauge in the prompt input.
Rules conflict with each other. Split conflicting rules into more specific glob patterns. A rule for src/api/**/*.ts and a rule for src/frontend/**/*.tsx will never conflict because they target different files.
Rules are too generic to be useful. The number one mistake: writing rules that sound like a style guide without concrete examples. Instead of “use proper error handling,” write “wrap all database calls in try/catch and throw AppError from @src/lib/errors.ts with the appropriate HTTP status code.”
Agent ignores rules. Agent-decided rules (alwaysApply: false with a description) depend on the agent determining relevance. If the rule is not being picked up for relevant tasks, either make it always-apply or add more specific glob patterns.
What’s Next
Section titled “What’s Next”- Team Collaboration — Share rules across your team and enforce standards
- Large Codebase Strategies — Rules are essential for large project navigation
- Token Management — Rules consume context tokens; optimize for relevance