Back to Tutorials

$ Defining Cursor Rules: Guide Your AI Like a Pro

Learn how to create custom rules to guide AI in generating code according to your standards, covering code style, security, performance, and error prevention.

In this tutorial, we'll dive into creating custom rules in Cursor AI and Windsurf AI to ensure the AI generates code that fits your standards. Whether you're enforcing a clean code style, boosting security, optimizing performance, or preventing errors, custom rules are your secret weapon for consistent, high-quality code. Let's get started and see how you can shape the AI to match your vision!

rules_overview.md
  • What Are Custom Rules Learn what custom rules are and why they're essential for consistent, high-quality code generation.
  • Setting Up Rules Step-by-step instructions for creating rule files in both Cursor AI and Windsurf AI.
  • Practical Examples Real-world scenarios showing how rules shape AI output for code style, security, performance, and error prevention.
  • Best Practices Tips for creating effective rules and ensuring the AI follows them consistently.
  • FAQs and Resources Common questions answered and links to valuable resources for rule inspiration.

What Are Custom Rules and Why Do They Matter?

Custom rules are like a cheat sheet for your AI, telling it how to generate code. They're stored in special files: .cursorrules for Cursor AI and .windsurfrules for Windsurf AI. These rules ensure your code stays consistent, secure, and efficient, saving you time and headaches.

Imagine your team agreeing on a style, and the AI sticks to it—no more debates over indentation! They cover everything from code style (like indentation) to security (like parameterized queries) to performance (minimizing loop operations) and error prevention (checking for null).

Setting Up Custom Rules in Cursor AI

Let's walk through creating and saving your rules in Cursor AI:

cursor_rules_setup.md
  • Step 1: Create the Rules File
    • Open your project in Cursor AI
    • In the file explorer, right-click in the root directory and select "New File"
    • Name the file .cursorrules
  • Step 2: Write Your Rules
    • Open the .cursorrules file and write your rules, one per line in plain text
    • For example:
      • "Use 4 spaces for indentation."
      • "Always use parameterized queries to prevent SQL injection."
      • "Minimize operations inside loops for better performance."
      • "Check for null before accessing properties to avoid crashes."
  • Step 3: Save and Test
    • Hit Ctrl + S (or Cmd + S on macOS) to save. No restart needed—the AI picks up changes immediately.
    • Test by generating code (e.g., type # Generate a function to add numbers) and check if it follows your rules.
# Example .cursorrules file contents

Use 4 spaces for indentation, not tabs.
Variable names should be in snake_case.
Always include error handling in functions.
Document all functions with clear comments.
Validate all user inputs to prevent security vulnerabilities.
Prefer async/await over callbacks for asynchronous code.
Minimize operations inside loops for better performance.
Use type hints in Python code.

Setting Up Custom Rules in Windsurf AI

Now, let's do the same for Windsurf AI, noting it might interpret rules slightly differently due to its proprietary models:

windsurf_rules_setup.md
  • Step 1: Create the Rules File
    • Open your project in Windsurf AI
    • In the file explorer, right-click in the root directory and select "New File"
    • Name the file .windsurfrules
  • Step 2: Write Your Rules
    • Open the .windsurfrules file and write your rules, one per line, like:
      • "Variable names should be in snake_case."
      • "Ensure all functions have docstrings for clarity."
      • "Use secure protocols for network communications."
  • Step 3: Save and Test
    • Save with Ctrl + S (or Cmd + S on macOS). The AI should apply these rules right away.
    • Test by generating code using Windsurf AI's features (like Cascade for multi-file tasks) and verify adherence.

Practical Examples: See Custom Rules in Action

Let's try some hands-on scenarios to see how rules shape AI output:

practical_examples.md
  • Enforcing Code Style

    Add "Use snake_case for all variable names" to both .cursorrules and .windsurfrules.

    # Generate a function to calculate area of a rectangle with given length and width

    Check if variables are rectangle_length and rectangle_width, not rectangleLength.

  • Ensuring Security

    Add "Always use parameterized queries to prevent SQL injection" and generate code for a database query:

    # Create a function to retrieve user data from the database

    Verify it uses parameterized queries, not string concatenation.

  • Optimizing Performance

    Add "Minimize operations inside loops" and generate a loop:

    # Generate a function to sum numbers from 1 to 100

    Check if it avoids expensive operations inside the loop.

  • Preventing Errors

    Add "Check for null before accessing properties" and generate code handling objects:

    # Create a function to get user name from an object

    Ensure it includes null checks before accessing properties.

Best Practices and Tips

  • Be Specific: Clear rules like "Use 4 spaces, not tabs" work better than vague ones.
  • Test Regularly: Generate code and check adherence. If the AI misses a rule, adjust your instructions.
  • Categorize Rules: Group them by style, security, performance, and error prevention for clarity.
  • Experiment with Windsurf AI: Since it uses different AI models, test how it handles rules compared to Cursor AI—it might surprise you!

Rule Categories Comparison

Tool Rule File Code Style Example Security Example Performance Example Error Prevention Example
Cursor AI .cursorrules Use 4 spaces for indentation. Always use parameterized queries. Minimize operations inside loops. Check for null before accessing properties.
Windsurf AI .windsurfrules Use 4 spaces for indentation. Always use parameterized queries. Minimize operations inside loops. Check for null before accessing properties.

Pro Tip: The AI uses natural language processing to understand your rules, so be precise and clear with your instructions. If you notice the AI isn't following a rule consistently, try rephrasing it to be more explicit or add examples directly in the rules file.

FAQ: Your Custom Rules Questions Answered

rules_faq.md
  • Q: What format should the rules be in?

    A: Plain text, one rule per line, no special syntax needed. Keep it simple and clear.

  • Q: How does the AI interpret the rules?

    A: The AI uses natural language processing to understand your rules and applies them during code generation. Be precise for best results.

  • Q: Can I have multiple rules in one file?

    A: Yes, list as many as you want, each on a new line.

  • Q: Do I need to restart the editor after saving?

    A: No, the AI picks up changes immediately after saving. If you don't see changes, regenerate code to test.

  • Q: How can I test if a rule is being followed?

    A: Generate code that should be affected by the rule and manually check the output. For example, check indentation or variable naming.

  • Q: Are there any specific syntax or keywords I should use?

    A: No, plain English is fine. Use technical terms appropriately, like "parameterized queries" for security.

  • Q: Can I have comments in the rules file?

    A: While not officially supported, you can add comments for reference, but keep the file mostly rules for clarity.

Sources for Useful Rules

Need inspiration? Check these trusted resources for rule ideas:

Further Reading