Cursor 初体验 & rules 推荐

8,047 阅读5分钟

Cursor 初体验 & rules 推荐

使用背景

  • 作为一名后端程序员,对前端不甚了解,有时却不得不需要定制化前端页面(学生党课设)
  • 快速完成后端项目的搭建
  • 对于一些简单通用的模块快速提供基本逻辑的实现
  • 进行代码诊断,发现 bug / 优化

于是打开了 Cursor 的大门。

基本用法

首先,在官网 www.cursor.com/ 下载。

默认需要 ctrl + i 调出与 AI 对话的功能,我们可以设置(as panel)成嵌入在右边的模式。

然后直接和 AI 对话即可,非常简单

  • 对于你接受的代码,选择 accept
  • 不符合预期的代码,选择 reject,尝试再优化你的意思的表达

image-20241116233914632

方便的版本控制:Checkpoint

在 accept all 以后,可能会有需要回滚的场景,比如 Cursor 生成的代码不符合预期。

每次在 Cursor 处理完你的问题后,都会生成一个 checkpoint,此时点击边上的 checkout,即可跳转到当前版本。而不需要自己 git commit,非常方便。

配置项

一般来说,Cursor 默认的配置就足够好用,当然你可以定制化自己需要的配置,配置项不多也不复杂,你可以在官网的 doc 中找到详细的说明。

我这里就说一下 features - Iterate on lints ,打开后有时可以自动识别代码错误并修正,但存在 bug,会一直重复提示一个不存在的编译错误,可能会使得 LLM 处理”支线“而非”主线“,所以看情况开关。

prompt 发挥的关键作用

prompt 是一种用于指导AI模型生成所需输出的“输入方式”,通常为一段文本或信息,在 Cursor 中也就是我们输入的所有文本。

虽然 Cursor 入门使用非常简单,但只有合适的 prompt 才能让 Cursor 做出符合你预期的行为。

如何优化 prompt

关键的问题是:如何把需求表达得足够清楚?怎样优化你的 prompt?

下面是我做的一个优化 prompt 的总结:

1、大多数模型对英文的理解能力更强,对于这些模型尽量使用英文交互;

2、用 xml、json 等 LLM 更易理解的数据格式;

3、用尽量少的 token ,表达最清晰的意思;

4、用列表的形式,列清优化的大方向,有助于引导 LLM;

5、请求 LLM 的文本和 LLM 返回的结果,都需要显式给出格式规范等;

6、长文本的结果往往会不准确,需要找准一个合理的经验值。

下面给出一个 prompt 的例子,来源于吴恩达的开源项目 Translation Agent,供大家参考:

 # 以列表的形式,强调了从准确性 (accuracy)、流畅度 (fluency)、风格 (style) 和术语 (terminology)四个方面来优化翻译结果。
 Your task is to carefully read a source text and a translation from {source_lang} to {target_lang}, and then give constructive criticism and helpful suggestions to improve the translation. \
 {country_specific}
 The source text and initial translation, delimited by XML tags <SOURCE_TEXT></SOURCE_TEXT> and <TRANSLATION></TRANSLATION>, are as follows:
 ​
 <SOURCE_TEXT>
 {source_text}
 </SOURCE_TEXT>
 ​
 <TRANSLATION>
 {translation_1}
 </TRANSLATION>
 ​
 When writing suggestions, pay attention to whether there are ways to improve the translation's \n\
 (i) accuracy (by correcting errors of addition, mistranslation, omission, or untranslated text),\n\
 (ii) fluency (by applying {target_lang} grammar, spelling and punctuation rules, and ensuring there are no unnecessary repetitions),\n\
 (iii) style (by ensuring the translations reflect the style of the source text and takes into account any cultural context),\n\
 (iv) terminology (by ensuring terminology use is consistent and reflects the source text domain; and by only ensuring you use equivalent idioms {target_lang}).\n\
 ​
 Write a list of specific, helpful and constructive suggestions for improving the translation.
 Each suggestion should address one specific part of the translation.
 Output only the suggestions and nothing else.

rules:优化你的 Cursor

现在你应该对如何写 prompt 有了一个初步的了解,但是,每次和 Cursor 交互都要加很多重复性的东西,未免太过麻烦,比如:

  • 请在关键步骤写注释
  • 请使用 SpringBoot2.7.2 版本
  • 请不要擅自修改 pom.xml

针对这一些通用的 prompt,Cursor 提供了一个解决方案:rules

Cursor 官方是这样介绍 rules 的:

 You can add custom instructions to Cursor by modifying the Rules for AI section under Cursor Settings > General > Rules for AI.

总之,rules 可以引导 Cursor 的表现,达到和 prompt 类似的效果。因此,针对一些通用的 prompt,只需要配置 rules 即可,不需要每次交互都携带上。

rules 有两种使用方式:全局粒度 rules 和项目粒度 rules。下面分别介绍使用方法。

1、全局 rule:Rules for AI

点击右上角设置 - General - Rules for AI 修改即可。

全局 rule 一般写全局通用的 rules,比如:

  • 代码注释规则
  • 输出关键日志信息

2、项目粒度:.cursorrules 文件

首先需要把 Rules for AI 下面的这个功能打开

image-20241116235656891

然后编写 .cursorrules 文件放在项目内即可。

修改 rules:reload window

如果中途修改了 rules,记得 reload window 以确保 rules 生效

image-20241119101057842

reload 不会影响你之前和 Composer 的所有对话,依然保留

rules 推荐

下面推荐一些 rules 供大家参考

 # 源自:https://www.youtube.com/watch?v=YozWFvkUMcQ
 DO NOT GIVE ME HIGH LEVEL STUFF, IF I ASK FOR FIX OR EXPLANATION, I WANT ACTUAL CODE OR EXPLANATION!!! I DON'T WANT "Here's how you can blablabla"
 ​
 - Be casual unless otherwise specified
 - Be terse
 - Suggest solutions that I didn’t think about—anticipate my needs
 - Treat me as an expert
 - Be accurate and thorough
 - Give the answer immediately. Provide detailed explanations and restate my query in your own words if necessary after giving the answer
 - Value good arguments over authorities, the source is irrelevant
 - Consider new technologies and contrarian ideas, not just the conventional wisdom
 - You may use high levels of speculation or prediction, just flag it for me
 - No moral lectures
 - Discuss safety only when it's crucial and non-obvious
 - If your content policy is an issue, provide the closest acceptable response and explain the content policy issue afterward
 - Cite sources whenever possible at the end, not inline
 - No need to mention your knowledge cutoff
 - No need to disclose you're an AI
 - Please respect my prettier preferences when you provide code.
 - Split into multiple responses if one response isn't enough to answer the question.
   If I ask for adjustments to code I have provided you, do not repeat all of my code unnecessarily. Instead try to keep the answer brief by giving just a couple lines before/after any changes you make. Multiple code blocks are ok.
 如果你使用 Java 构建后端,请遵循以下需求:
 - 使用 Java8(JDK1.8)
 - 使用 SpringBoot2.7.2
 - SpringBoot 的配置文件使用 application.yml,而不要用 application.properties
 - 使用 maven 进行项目依赖管理
 - 使用 MyBatis / MyBatis-Plus 进行数据库操作
 - 可以使用 Lombok
 - 使用标准的错误响应格式
 - 在合适的地方加注释,目标是只看注释就能大概知道一个方法的作用
 - 严格禁止使用 baseMapper ,必须属性注入 mapper 再进行操作
 - 前后端交互时,在处理时间格式时需要特别注意