Packs

How to write Cursor rules: project rules and .mdc files, with examples

Guide · checked against the docs on

Cursor rules are instructions that Cursor's Agent reads every time, so you don't have to repeat yourself in chat. This guide shows where project rules live, the one file-format detail that trips people up, the four ways a rule can be applied, and what Cursor says to leave out. Everything here comes from Cursor's rules docs, read on October 15, 2026.

Not affiliated with or endorsed by Anysphere (Cursor). Check Cursor's current docs before relying on these details.

Our tool page: Cursor

What rules do

Cursor's docs explain why rules exist: "Large language models don't retain memory between completions. Rules provide persistent, reusable context at the prompt level. When applied, rule contents are included at the start of the model context." (checked Oct 2026)

The docs list four kinds:

KindWhere it lives
Project Rules"Stored in .cursor/rules, version-controlled and scoped to your codebase." (checked Oct 2026)
User Rules"Global to your Cursor environment. Used by Agent (Chat)." (checked Oct 2026)
Team Rules"Team-wide rules managed from the dashboard. Available on Team and Enterprise plans." (checked Oct 2026)
AGENTS.md"Agent instructions in markdown format. Simple alternative to .cursor/rules." (checked Oct 2026)

This guide covers project rules. For AGENTS.md, see our AGENTS.md and CLAUDE.md guide.

The .mdc rule

Cursor's docs say: "Project rules must use the .mdc extension. A plain .md file in .cursor/rules is ignored by the rules system because it has no frontmatter to specify description, globs, and alwaysApply." (checked Oct 2026) If you'd rather write plain markdown, the docs say to "use AGENTS.md instead." (checked Oct 2026)

You can name rule files anything and organize them in folders. The docs show this layout:

.cursor/rules/
  react-patterns.mdc     # Recognized as a project rule
  api-guidelines.md      # Ignored (wrong extension)
  frontend/              # Organize rules in folders
    components.mdc

The four rule types

Each rule is a markdown file with frontmatter at the top. Three fields, alwaysApply, description and globs, decide when Cursor uses it. Here's the docs' table:

alwaysApplydescriptionglobsWhat happens
true--"Always included. Globs and description are ignored." (checked Oct 2026)
false-provided"Auto-attached when a matching file is in context." (checked Oct 2026)
falseprovidedomitted"Agent reads the description and pulls the rule in when relevant." (checked Oct 2026)
falseomittedomitted"Included only when you @-mention the rule in chat." (checked Oct 2026)

In Cursor's menu these are called Always Apply, Apply to Specific Files, Apply Intelligently and Apply Manually (checked Oct 2026).

Always apply (NOT TESTED)

---
alwaysApply: true
---
- Never modify generated files in the `dist/` or `build/` directories
- When unsure about implementation details, read the relevant source files before proposing changes

Apply to specific files (NOT TESTED)

---
globs: src/components/**/*.tsx
alwaysApply: false
---
- Use named exports, not default exports
- Keep components under 200 lines

Apply intelligently (NOT TESTED)

---
description: RPC service conventions and patterns for the backend
alwaysApply: false
---
- Define each service in its own file under `src/services/`
- Validate inputs at the service boundary

Apply manually (NOT TESTED)

---
alwaysApply: false
---
- Every database migration must have both `up` and `down` functions

A down migration isn't a backup. Prisma's docs say a down migration "will revert your database schema, but other changes to data and application code that are carried out as part of the up migration will not be reverted." (checked Oct 2026) Our advice, not a vendor quote: back up the database before you run a migration.

You'd use this one by typing @ and the rule's name in chat, for example @my-rule.

These examples are shortened from the ones in Cursor's docs.

Glob patterns

Globs pick which files a rule applies to. The docs say to "Separate multiple patterns with commas." (checked Oct 2026) A few from the docs' table:

PatternMatches
**/*.ts"All .ts files in any directory" (checked Oct 2026)
src/**"All files anywhere under src/" (checked Oct 2026)
src/**/*.tsx"All .tsx files anywhere under src/" (checked Oct 2026)
docs/**/*.md, docs/**/*.mdx".md and .mdx files under docs/ (comma-separated)" (checked Oct 2026)

Pointing to files instead of pasting them

A rule can mention a file, like @migration-template.sql. The docs say "Agent reads the file with its tools when it needs the content. The file's contents are not inlined into the prompt" (checked Oct 2026). So "Put anything that must always be in context directly in the rule body." (checked Oct 2026)

Creating a rule

The docs give two ways:

What Cursor says to keep out of rules

Cursor's tips: "Keep rules under 500 lines" (checked Oct 2026), "Split large rules into multiple, composable rules" (checked Oct 2026), and "Avoid vague guidance." (checked Oct 2026) The docs also list what to avoid:

Never put secrets in a rule. Our advice, not a vendor quote: Project rules are version-controlled, so they usually end up in your repo, and when a rule applies, it's added to the model's context. Keep API keys in a .env file instead. See our guide to keeping secrets safe with AI coding tools.

Not affiliated with or endorsed by Anysphere (Cursor). Check Cursor's current docs before relying on these details.