📋 What are Cursor Rules
Cursor Rules is the rule configuration system of the Cursor editor, which is used to guide AI to follow specific specifications and standards during the code generation process. By defining your project background, coding standards, library preferences, etc., you can ensure that the code generated by Cursor is closely aligned with your project needs.
Simply put, Rules are the "code of conduct" for Cursor AI:
- Tell AI what your project is about
- Specifies what style the code should follow
- Specify which libraries and frameworks to use
- Set output language and format preferences
🎯 Rule type
Cursor Rules are divided into two levels:
1. User Rules (Global Rules)
Location:
Cursor Settings → Rules
General rules that apply to all projects, such as:
- Output language preference (Chinese/English)
- Response length limit
- Unified code style
- General best practices
# 语言设置 - 所有对话和注释使用中文 - 代码中的变量名和函数名使用英文 # 代码风格 - 使用 2 空格缩进 - 使用单引号而非双引号(JavaScript/TypeScript) - 函数优先使用箭头函数 # 输出格式 - 回复简洁明了,避免冗余解释 - 代码块要标注语言类型 - 重要更改要说明原因
2. Project Rules
Location:
.cursor/rules/
in the directory
.mdc
File
Rules for specific projects, supporting multiple trigger modes:
| Rule Type | Trigger mode | Applicable scenarios |
|---|---|---|
Always |
Automatically carried in every conversation | Project core specifications and architecture description |
Auto Attached |
Matching file types automatically take effect | Language-specific coding conventions |
Agent Request |
Agent automatically determines whether it is valid | Implementation Guidelines for Specific Functions |
Manual |
Manual @ Reference | Special rules for occasional use |
⚙️ Configuration method
Method 1: Set the interface through Cursor
1. 打开 Cursor 设置(Cmd/Ctrl + ,) 2. 选择 "Rules" 标签页 3. 在 "Project Rules" 区域点击 "Add new rule" 4. 指定规则名称,按 Enter 键 5. Cursor 会自动创建 .cursor/rules 文件夹 6. 编辑生成的 .mdc 文件,添加规则内容
Method 2: Manually create files
# 创建规则目录 mkdir -p .cursor/rules # 创建全局规则文件 touch .cursor/rules/global.mdc # 创建特定语言规则 touch .cursor/rules/typescript.mdc
.mdc file structure
--- description: 项目全局规则 globs: alwaysApply: true --- # 项目背景 这是一个使用 Next.js 14 + TypeScript 开发的 SaaS 应用。 # 技术栈 - 框架:Next.js 14 (App Router) - 语言:TypeScript 5.x - 样式:Tailwind CSS + shadcn/ui - 状态管理:Zustand - 数据获取:TanStack Query # 编码规范 - 组件使用函数式组件 + Hooks - 使用 TypeScript 严格模式 - 遵循 Airbnb JavaScript 风格指南 # 文件结构 - app/: 页面路由 - components/: 可复用组件 - lib/: 工具函数和配置 - hooks/: 自定义 Hooks - types/: TypeScript 类型定义
📄 Best Practice Template
React/Next.js project
# Next.js App Router 最佳实践 ## 组件规范 - 默认使用 Server Components - 仅在需要交互时使用 Client Components(添加 'use client') - 使用 layout.tsx 共享布局 - 使用 loading.tsx 处理加载状态 - 使用 error.tsx 处理错误 ## 数据获取 - Server Components 中直接 async/await 获取数据 - Client Components 使用 TanStack Query - 避免在客户端请求敏感数据 ## 样式规范 - 使用 Tailwind CSS 工具类 - 复杂样式抽取为组件 - 使用 cn() 函数合并类名 - 响应式设计遵循移动优先 ## 性能优化 - 图片使用 next/image - 字体使用 next/font - 适当使用 React.memo 和 useMemo - 大型列表使用虚拟滚动
Python Project
# Python 项目规范 ## 代码风格 - 遵循 PEP 8 规范 - 使用 Black 格式化代码 - 使用 isort 排序导入 - 最大行长度 88 字符 ## 类型注解 - 所有函数必须有类型注解 - 使用 typing 模块的高级类型 - 复杂类型使用 TypeAlias ## 文档规范 - 所有公共函数必须有 docstring - 使用 Google 风格的 docstring - 包含参数说明、返回值、异常 ## 测试 - 使用 pytest 框架 - 测试文件命名 test_*.py - 测试函数命名 test_* - 保持测试覆盖率 > 80%
💡 Tips
Help the AI understand the context by clearly describing the project background at the beginning of the rule. Including project type, target users, core functions, etc.
Clearly mark in the rules which files/code can be modified and which core logic should not be touched.
Create specialized rules for different file types. For example
*.test.ts
File automatically applies test specifications.
Rules should not be too long. Overly detailed rules will occupy context space and affect AI understanding. Keep the rules simple and focused.
Directory structure and technology stack files
It is recommended to maintain two files in the project root directory and reference them in the rules:
project/ ├── .cursor/ │ └── rules/ │ ├── global.mdc │ └── typescript.mdc ├── directorystructure.md # 目录结构说明 ├── technologystack.md # 技术栈说明 └── ...
📚 Related Resources
- Cursor Directory - High-quality Rules templates shared by the community
- Cursor official documentation - Rules User Guide
- Awesome CursorRules - Selected Rules Collection