Reuse Existing Code

Configure Cursor’s AI to always search for existing code before generating new solutions. This rule helps prevent duplicate implementations across your codebase.

Add the AI Rule

  1. Open Command Palette (Cmd + Shift + P)
  2. Select New Cursor Rule
  3. Add this instruction for the AI:
INSTRUCTION TO AI:
Before generating or suggesting any new code implementation, you MUST:

1. First rephrase the request as: "I am looking for code that does [requested functionality], is there existing code that can do this?"
2. Perform a semantic search in the codebase for similar solutions
3. Only proceed with generating new code if no suitable existing solution is found
4. If creating new code, explain why existing solutions weren't suitable

Remember: Your primary goal is to help maintain a cohesive codebase by finding and reusing existing code patterns.

How It Works

When you ask the AI:

"Write a function that handles user authentication"

The AI will first search:

"I am looking for code that handles user authentication, is there existing code that can do this?"

Was this page helpful?