返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

Cursor Rules 最佳实践总结

[复制链接]
链载Ai 显示全部楼层 发表于 昨天 12:47 |阅读模式 打印 上一主题 下一主题

ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;font-size: 16px;line-height: 1.6;letter-spacing: 1.4px;text-align: justify;">ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;line-height: 1.6;color: rgb(15, 76, 129);font-weight: bold;font-size: inherit;">见字如面,与大家分享实践中的经验与思考。

ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;font-size: 16px;line-height: 1.6;letter-spacing: 1.4px;text-align: justify;">使用 AI 编写代码的一大问题就是 AI 乱改,Cursor 引入了一项功能,称为 Cursor Rules。它能允许开发者规范 AI 的行为,提供更好的编码体验。让我们深入了解 Cursor Rules 是什么,以及为什么它们对现代开发工作流程至关重要。

ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;display: table;color: rgb(255, 255, 255);background: rgb(15, 76, 129);font-size: 20.8px;text-align: center;line-height: 1.75;border-radius: 8px 24px;box-shadow: rgba(0, 0, 0, 0.06) 0px 2px 6px;">什么是 Cursor Rules?

ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;font-size: 16px;line-height: 1.6;letter-spacing: 1.4px;text-align: justify;">Cursor Rules 用于自定义 AI 在 Cursor 中的行为,可以视为对大型语言模型(LLM)的指令或系统提示。Cursor支持两种类型的规则设置:

    ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;font-size: 16px;line-height: 1.6;list-style: decimal;" class="list-paddingleft-1">
  1. ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;line-height: 1.6;text-indent: 0px;display: list-item;color: rgb(0, 0, 0);">

    全局规则:在Cursor设置中的ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;line-height: 1.6;color: rgb(199, 37, 78);background-color: rgba(0, 0, 0, 0.04);border-radius: 3px;font-size: 14.4px;white-space: normal;">Rules > User Rules设置,适用于所有项目。

  2. ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;line-height: 1.6;text-indent: 0px;display: list-item;color: rgb(0, 0, 0);">

    项目规则:通过ingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;line-height: 1.6;color: rgb(199, 37, 78);background-color: rgba(0, 0, 0, 0.04);border-radius: 3px;font-size: 14.4px;white-space: normal;">.cursor/rules目录中的.mdc文件或项目根目录的.cursorrules文件设置。

官方建议采用 Project Rules 的.cursor/rules目录来管理规则文件,.cursorrules文件将在未来版本被废弃。

最佳实践

image-20250315下午71201421

如果你现在采用的是.cursorrules文件,可以将其分类拆分到不同的文件当中。我现在以一个实际项目,Cursor 版本为 0.47.5 为例。

01 通用规则

新建.cursor/rules.general.mdc文件,包含项目的基本信息和通用规范。设置RuleType为Always,将在所有的聊天框中应用。主要包含:技术栈、代码风格、项目结构等通用信息。

image-20250315下午71734752

02 Python 规则

该项目主要是python为主的语言,根据项目实际需求可以换成Java/React/Typescript等。新建一个.cursor/rules/python.mdc文件,Rule Type 采用 Auto Attached,文件后缀为*.py。主要包含Python的编码规范和最佳实践等。

image-20250315下午72042822

03 文档规则

新建一个.cursor/rules/document.mdc,针对文档文件的规范。

image-20250315下午72214091

04 Git 规则

新建一个.cursor/rules/git.mdc文件,针对Git操作的规范,包含提交规范、分支管理等。

image-20250315下午72324961

最后

  • 如果您是新用户,直接使用Project Rules。
  • 如果您有现有的.cursorrules文件,尽快迁移到Project Rules。
  • 使用Project Rules的高级功能来更精确地控制AI行为。
  • 将规则文件纳入版本控制,与团队共享。
  • 根据项目需求创建多个规则文件,而不是将所有规则放在一个文件中。
  • 规则需要定时进行 Review 和 改进。

最后就算你使用了Cursor Rules,有时候它不一定会按照你的规则进行编写代码。就像我规则中写了不要自动提交git代码,它有时候偶尔还是会改动完成后,你还没有accept这些文件,却被直接提交到了远程仓库。


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ