Cursor 101

Cursor Rules

学习如何使用 Cursor Rules来优化你的编码流程,提升开发效率。

查看官方文档

什么是 Cursor Rules

Cursor Rules帮助你定制 AI 行为,使其符合你的编码风格和项目需求。Cursor 提供两种类型的规则。

全局规则

在 Cursor 设置的"通用" > "AI 规则"中配置。这些规则适用于你的所有项目,非常适合设置个人编码偏好和保持 AI 行为的一致性。

项目规则

以 .mdc 格式存储在 .cursor/rules 目录中的项目特定规则。这些规则帮助 AI 理解你的代码库并遵循项目规范。通过 Cursor 设置 > 通用 > 项目规则来添加。它们会与你的代码库同步,并可以在需要时自动包含。

Cursor 项目规则界面展示规则管理面板

规则示例

全局规则

# 在 Cursor 设置 > AI 规则中

- 所有新代码使用 TypeScript
- 遵循代码整洁之道
- 优先使用 async/await 而不是回调
- 编写完善的错误处理
.mdc

项目规则 (example.mdc)

# 文件模式:*.tsx, *.ts

## React 规范
- 使用函数组件
- 实现完整的 prop 类型
- 遵循 React 最佳实践

## 样式
- 使用 Tailwind CSS
- 遵循团队样式指南

@file ../tsconfig.json
@file ../tailwind.config.js
.mdc

实施 Cursor Rules的主要优势

Increased Productivity

Cursor Rules automate repetitive tasks, allowing you to focus on writing quality code.

Consistent Code Style

Enforce coding standards across your team, ensuring a uniform and readable codebase.

Reduced Errors

Catch common mistakes early in the development process, saving time on debugging.

Faster Onboarding

New team members can quickly adapt to your project's coding style and best practices.

工作原理

语义化描述

每条规则都可以包含应用场景的描述,让规则更易理解和维护。

文件匹配模式

使用 glob 模式来指定规则适用的文件或目录,精确控制规则的应用范围。

自动附加

当引用匹配的文件时,规则会自动被包含,使工作流程更加流畅。

引用文件

在项目规则中使用 @file 来引用文件,在应用规则时提供更丰富的上下文信息。

如何创建 Cursor Rule

  1. 配置全局规则

    进入 Cursor 设置 > 通用 > AI 规则,设置适用于所有项目的规则。

  2. 配置项目规则

    进入 Cursor 设置 > 通用 > 项目规则,管理项目特定的规则。

    1. 创建新规则

      点击"添加规则"创建新的 .mdc 文件。编辑 Description(帮助 AI 代理选择规则)和 Globs(例如 *.tsx,用于自动包含规则)。

    2. 编写规则内容

      在 .mdc 文件中编写你的规则。Cursor 会根据你的 Description 和 Globs 设置自动同步和应用这些规则。

Cursor 项目规则界面展示如何添加和配置规则

Cursor 设置中的项目规则界面

  • • Description: AI 代理会根据描述来选择使用哪些规则
  • • Globs: 当你在这里指定文件模式(例如 *.tsx)时,这条规则会自动包含在匹配这些模式的文件的 AI 响应中

How to Create Effective Cursor Rules

What Makes a Good Rule

  • Easy to understand - avoid complex jargon
  • Specific to your project or coding style
  • Matches your team's agreed-upon practices
  • Works for different parts of your code
  • Simple to update as your project grows

Tips for Writing Better Rules

  • Begin with basic rules, then add more as needed
  • Write as if you're explaining to a colleague
  • Include examples from your own code
  • Think about how the rule applies to your project
  • Check and update your rules regularly

Making Your Rules Work Better

  • Use words and terms you use in your daily coding
  • Briefly describe your project or feature
  • Show short code examples when it helps
  • Mention any specific requirements or limitations
  • Ask for explanations when you need them

Cursor Rules示例

探索这些 Cursor Rules示例,为你自定义 AI 规则提供灵感。请记住,最有效的 Cursor Rules是根据你的具体项目需求和编码要求量身定制的。

typescript
javascript

TypeScript Expert

typescript
javascript

You are a TypeScript expert with deep knowledge of the language's features and best practices. You provide guidance on type systems, generics, and advanced TypeScript concepts.

获取更多 Cursor Rules示例和灵感,请访问Cursor Rules目录

Cursor Rules常见问题

获取更多关于 Cursor Rules的详细信息,请参考Cursor AI 规则官方文档

Ready to Supercharge Your Coding?

Discover how Cursor can revolutionize your development workflow. Watch our video tutorials to see all of Cursor's powerful features in action.

Don't miss out on the power of Cursor Rules. Start optimizing your workflow today!

Explore Video Tutorials