如何編寫 Cursor rules(.mdc):存放位置與內容
How to write Cursor rules (.mdc): where they go and what to put in them
Cursor 專案規則以 `.mdc` 檔案存放於 `.cursor/rules/`,並可按四種方式套用。四種方式包括每次對話套用、由 Agent 按描述判斷、按檔案 glob 套用,以及在對話中以 @ 手動提及;純 `.md` 檔案在該目錄會被忽略,可改用 `AGENTS.md`。
Cursor rules tell the agent how your project works. This page shows where they go, how the four rule types differ, and how to write one. The facts come from Cursor's rules docs, read on Nov 25, 2026. Check them for your version.
Where project rules live
Project rules live in .cursor/rules/ as .mdc files. They are version controlled, so check them into git.
One trap: a plain .md file in .cursor/rules is ignored, because it has no frontmatter. If you prefer plain Markdown, use AGENTS.md instead.
The four rule types
| Type | When it applies |
|---|---|
| Always Apply | Every chat session |
| Apply Intelligently | When the agent decides it is relevant, based on the description
|
| Apply to Specific Files | When a file matches the globs pattern |
| Apply Manually | When you @-mention the rule in chat |
The frontmatter fields are description, globs and alwaysApply.
An example
This rule applies only to Python files in one folder:
---
description: API error response conventions
globs: app/routers/**/*.py
alwaysApply: false
---
- Use HTTPException with a detail message that has no internal details.
- Return 400 for bad input, 404 for a missing item, 409 for a conflict.
And this one is always on, so keep it very short:
---
description: Plain writing for docs and commits
alwaysApply: true
---
- Start with the answer. No warm-up line.
- Use plain words. No hype. Give numbers, not adjectives.
- No em dashes. No emoji at the start of list items.
Tips from the Cursor docs
- Keep each rule under 500 lines. Split big rules into smaller ones.
- Give a concrete example, or point to a file instead of copying its text.
- Do not copy a whole style guide. Use a linter.
- Do not document every command. The agent already knows
npm,gitandpytest. - Add a rule when you see the agent make the same mistake twice, not before.
Always-on rules cost tokens
A rule with alwaysApply: true goes into every chat. Give most rules a globs pattern or a description so they load only when needed. Our free Agent Cost Saver has a script, cost_audit.py, that adds up your always-on rules and tells you which to scope.
A free rule to start with
Anti-Slop Rules has a ready .mdc file (templates/anti-slop.mdc) that keeps the agent's READMEs, commits and PR text plain. Copy it to .cursor/rules/. It is MIT licensed.
Related guides
Made by Tidy Byte Tools.
Originally published at https://tidybytetools.github.io/guides/write-cursor-rules/. Tools by Tidy Byte Tools.
來源:DEV Community · dev.to