Cursor · Coding

Code generation

Point Agent at an example to copy, build from a screenshot or mockup, and use tests as the target.

Generating code in Cursor

Decide what done looks like first

Generation goes wrong in two ways: the code doesn't fit your repo, or it doesn't do what you meant. A reference file solves the first, and a test or a design solves the second. Most real requests need one of each, such as a new endpoint copying an existing one plus tests that define its behavior.

Keep requests small enough to read

Agent can produce hundreds of lines quickly, but you still have to understand them. If a feature is large, use Plan mode to break it up, and build one step at a time. Cursor's course says it's often faster to iterate in smaller follow-up conversations once the first one is under way.

Frequently asked
How do I get Cursor to match my code style?

@-mention an existing file as the model to follow, and write lasting conventions as project rules. A linter is still better for pure formatting, as Cursor's docs advise against pasting whole style guides into rules.

Can Cursor turn a screenshot into code?

Yes. Drag an image into the Agent input or paste it, and ask it to build the component using your existing parts. On vision-capable models it reads the image directly, and the built-in browser lets it check the result.

Should I write tests first with Cursor?

For logic with clear inputs and outputs, it works well. Have Agent write failing tests, commit them, then tell it to make them pass without modifying them, which gives it feedback on every run.