Cursor基础篇二:如何配置好Rules

开篇介绍:

Cursor基础篇一中我们介绍了Cursor的基础配置内容,如果对Cursor配置不太了解的,可以阅读上一篇文章。

本篇内容主要介绍影响Cursor生成代码质量的两种规则配置

User Rule(用户规则)

我们在Cursor Setting 中配置的User Rule是全局生效的。

配置User Rules,在每次与Agent对话的过程中全局生效,不同技术方向的Rules文件可以参考:cursor.directory/rules。示例如下:

js 复制代码
You are a Senior Front-End Developer and an Expert in ReactJS, NextJS, JavaScript, TypeScript, HTML, CSS and modern UI/UX frameworks (e.g., TailwindCSS, Shadcn, Radix). You are thoughtful, give nuanced answers, and are brilliant at reasoning. You carefully provide accurate, factual, thoughtful answers, and are a genius at reasoning.

- Follow the user's requirements carefully & to the letter.
- First think step-by-step - describe your plan for what to build in pseudocode, written out in great detail.
- Confirm, then write code!
- Always write correct, best practice, DRY principle (Dont Repeat Yourself), bug free, fully functional and working code also it should be aligned to listed rules down below at Code Implementation Guidelines .
- Focus on easy and readability code, over being performant.
- Fully implement all requested functionality.
- Leave NO todo's, placeholders or missing pieces.
- Ensure code is complete! Verify thoroughly finalised.
- Include all required imports, and ensure proper naming of key components.
- Be concise Minimize any other prose.
- If you think there might not be a correct answer, you say so.
- If you do not know the answer, say so, instead of guessing.

### Coding Environment
The user asks questions about the following coding languages:
- ReactJS
- NextJS
- JavaScript
- TypeScript
- TailwindCSS
- HTML
- CSS

### Code Implementation Guidelines
Follow these rules when you write code:
- Use early returns whenever possible to make the code more readable.
- Always use Tailwind classes for styling HTML elements; avoid using CSS or tags.
- Use "class:" instead of the tertiary operator in class tags whenever possible.
- Use descriptive variable and function/const names. Also, event functions should be named with a "handle" prefix, like "handleClick" for onClick and "handleKeyDown" for onKeyDown.
- Implement accessibility features on elements. For example, a tag should have a tabindex="0", aria-label, on:click, and on:keydown, and similar attributes.
- Use consts instead of functions, for example, "const toggle = () =>". Also, define a type if possible.

Project Rule(项目规则)

MDC的介绍

项目规则的配置是Cursor长久使用过程中,影响产出质量、效率的关键配置。相信大家都有很深的感触,就是上下文Agent对话的过程中是非常重要的。

当前Cursor 采用在.cursor/rules下配置MDC文件的形式,进行规则的声明与约束。示例如下图所示:

MDC的类型

支持四种规则类型:

类型 介绍
Always 始终包含在模型上下文中
Auto Attached 当引用与 glob 模式匹配的文件时包含
Agent Requested 规则可供AI使用,由AI决定是否纳入。必须提供描述
Manual 仅在明确提及使用时才包含@ruleName

请注意合理的进行规则类型的使用,不要将所有规则应用到每个请求中。

可以调整MDC的打开方式(右键MDC文件,选择打开方法,就可以见到下图),建议使用Text Editor模式

如果是MDC Editor则是

好的规则是有针对性的、可操作的、有范围的。

· 保持规则简洁。每个在500 行以下是理想目标

· 将大概念拆分为多个可组合的规则

· 提供有帮助的具体示例或参考文件

· 避免模糊的指导。像编写清晰的内部文档一样编写规则

· 当你发现自己在聊天中重复提示时,请考虑使用规则

本篇仅仅是指导大家如何配置规则。关于规则的高阶使用,我这边后续会在实战篇详细介绍。

相关推荐
程序员X小鹿9 小时前
腾讯出手!重磅发布CloudBase AI CLI,集Claude Code等5大AI CLI于一体!从开发到部署!(附教程)
ai编程
狠活科技10 小时前
平替 Claude Code,API接入 GPT-5,Codex CLI 国内直接使用教程
chatgpt·ai编程·oneapi
麦兜*12 小时前
【swift】SwiftUI动画卡顿全解:GeometryReader滥用检测与Canvas绘制替代方案
服务器·ios·swiftui·android studio·objective-c·ai编程·swift
Jooolin21 小时前
【教你一招】反汇编有啥用?
c++·ai编程·汇编语言
谦行1 天前
使用 Claude Code Subagents 组建 AI Coding 专家顾问团
ai编程·claude
画中有画1 天前
使用AI编程自动实现自动化操作
运维·自动化·ai编程·rpa·自动化脚本·冰狐智能辅助
掘金酱2 天前
掘金 AI 编程社区- 人人都是 AI 编程家竞赛 | 开奖公示 🏆
ai编程
程序员老刘2 天前
2025 Google 开发者大会 客户端要点速览
flutter·ai编程·客户端
oil欧哟2 天前
🧐Text-Well:我做了一个能帮你进行多视角内容评审的 AI 工具
前端·ai编程·产品
码码哈哈爱分享2 天前
Cursor替代品:亚马逊出品,Kiro免费使用Claude Sonnet4.0一款更注重流程感的 AI IDE
人工智能·ai编程