Best practice L2 · Context engineering informational

Scope Cursor's Generation to a File and an Existing Pattern

What it is

Generating new code in Cursor by scoping the request to a specific file and pointing at an existing example to copy the pattern from — rather than asking for code in the abstract.

pattern-matched generationName the filePoint to a patternGenerate to match
Point Cursor at a good example so new code matches the old.

Why it works

Cursor can see your codebase, so the most reliable instruction is 'do it like this'. Anchoring generation to a real pattern makes the new code fit your conventions on the first try, instead of arriving in a house style you then have to rewrite.

When to use it

Adding another instance of something the codebase already does: a new route, a new component, another test in the same shape. Best when a good example already exists to mirror.

When not to use it

Genuinely novel code with no precedent in the repo — there you're designing, not pattern-matching, so lead with the interface instead.

Prompt

In <target file>, add <new thing>. Follow the exact pattern used in <reference file/symbol> — same structure, naming, and error handling. Only add what's needed; don't restyle the surrounding code.

Example

Pointed at an existing usersController, Cursor generates a matching ordersController with the same validation, error shape, and naming — a drop-in that reads like the rest of the code.

Advanced version

When several files share a pattern, mention the best one explicitly so Cursor copies the canonical version rather than averaging an inconsistent set.

Common mistakes

  • Asking for code with no reference, then reformatting it to match the repo by hand.
  • Pointing at a bad example, so Cursor faithfully propagates the thing you should be fixing.
  • Letting generation quietly restyle neighbouring code it was only meant to extend.

Related