create-rule Skill
创建 Cursor 规则以提供持久的 AI 指导。当用户想创建规则、添加编码规范、项目约定、特定文件模式,或提到 ".cursor/rules"、"AGENTS.md"、"cursor rule"、“coding standard”、“convention”或“AI guidance”时使用。
安装方式:把技能目录放入 ~/.claude/skills/(Claude Code)或在 claude.ai 设置中启用;也可复制右侧安装命令一键添加。
技能指令原文(SKILL.md)
Creating Cursor Rules
Create project rules in .cursor/rules/ to provide persistent context for the AI agent.
For one plain-Markdown file that always applies, AGENTS.md at the project root does the same job as an alwaysApply rule and needs no frontmatter.
Check existing first
Before creating a rule, check:
- Check for existing rules:
ls -la .cursor/rules/*.mdc 2>/dev/null
cat .cursor/rules/*.mdc 2>/dev/null | head -100
- Check for conflicting conventions:
cat CONTRIBUTING.md .editorconfig .eslintrc* 2>/dev/null | head -50
- Check if similar rule exists:
- Don't duplicate existing rules
- Consider extending existing rules instead
Why: Rules should complement, not conflict with, existing project conventions.
Gather Requirements
Before creating a rule, determine:
- Purpose: What should this rule enforce or teach?
- Scope: Should it always apply, or only for specific files?
- File patterns: If file-specific, which glob patterns?
Inferring from Context
If you have previous conversation context, infer rules from what was discussed. You can create multiple rules if the conversation covers distinct topics or patterns. Don't ask redundant questions if the context already provides the answers.
Required Questions
If the user hasn't specified scope, ask:
- "Should this rule always apply, or only when working with specific files?"
If they mentioned specific files and haven't provided concrete patterns, ask:
- "Which file patterns should this rule apply to?" (e.g.,
/.ts,backend//.py)
Get the file patterns right: a wrong glob means the rule never loads, or loads for every file.
Use the AskQuestion tool when available to gather this efficiently.
Rule File Format
Rules are .mdc files in .cursor/rules/ with YAML frontmatter:
.cursor/rules/
typescript-standards.mdc
react-patterns.mdc
api-conventions.mdc
File Structure
---
description: Brief description of what this rule does
globs: **/*.ts # File pattern for file-specific rules
alwaysApply: false # Set to true if rule should always apply
---
# Rule Title
Your rule content here...
Frontmatter Fields
| Field | Type | Description |
|-------|------|-------------|
| description | string | What the rule does (shown in rule picker) |
| globs | string | File pattern - rule applies when matching files are open |
| alwaysApply | boolean | If true, applies to every session |
Rule Configurations
Always Apply
For universal standards that should apply to every conversation:
---
description: Core coding standards for the project
alwaysApply: true
---
Apply to Specific Files
For rules that apply when working with certain file types:
---
description: TypeScript conventions for this project
globs: **/*.ts
alwaysApply: false
---
Best Practices
Keep Rules Concise
- Under 50 lines: Rules should be concise and to the point
- One concern per rule: Split large rules into focused pieces
- Actionable: Write like clear internal docs
- Normal volume, with the reason: state each constraint once and say why. MUST/NEVER/CRITICAL over-trigger on current models; "try to" reads as permission to skip
- Concrete examples: Ideally provide concrete examples of how to fix issues
- Always-on costs every turn: an
alwaysApplyrule rides in every request inside the cached prefix. Keep those few, short, and byte-stable (no dates, counters, or generated timestamps); scope the rest withglobs
Example Rules
TypeScript Standards
---
description: TypeScript coding standards
globs: **/*.ts
alwaysApply: false
---
# Error Handling
\`\`\`typescript
// ❌ BAD
try {
await fetchData();
} catch (e) {}
// ✅ GOOD
try {
await fetchData();
} catch (e) {
logger.error('Failed to fetch', { error: e });
throw new DataFetchError('Unable to retrieve data', { cause: e });
}
\`\`\`
React Patterns
---
description: React component patterns
globs: **/*.tsx
alwaysApply: false
---
# React Patterns
- Use functional components
- Extract custom hooks for reusable logic
- Colocate styles with components
Checklist
- [ ] File is
.mdcformat in.cursor/rules/ - [ ] Frontmatter configured correctly
- [ ] Content under 50 lines (split larger rules by concern)
- [ ] Includes concrete examples