Skip to content

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.

  • 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

Cursor supports four rule application modes:

TypeFrontmatterWhen AppliedUse For
Always ApplyalwaysApply: trueEvery chat sessionCore coding standards, file conventions
Apply IntelligentlyalwaysApply: false + descriptionAgent decides based on relevanceDomain-specific patterns, workflow guides
Glob-Scopedglobs: "src/api/**/*.ts"When matching files are in contextDirectory-specific conventions
ManualNo frontmatter or alwaysApply: false without descriptionWhen @rule-name is mentionedSpecialized workflows, templates

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)

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 conditionals

Rule 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-end
alwaysApply: false
---
When implementing a new feature:
1. Database: Create migration in src/db/migrations/ using Drizzle
2. 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.
---
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.

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 endpoint
alwaysApply: 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 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

When rules conflict, they are applied in this order:

  1. Team Rules (highest precedence)
  2. Project Rules (.cursor/rules/)
  3. User Rules (personal preferences)

This means a team admin can enforce standards that individual developers cannot override.

Import rules directly from GitHub repositories:

  1. Open Cursor Settings > Rules, Commands
  2. Click + Add Rule next to Project Rules
  3. Select Remote Rule (GitHub)
  4. Paste the repository URL

Imported rules stay synced with the source repository. Updates to the remote rule are automatically reflected in your project.

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.

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.