How to Write a Cursor Rule: The Complete 2026 Guide
Learn how to create effective Cursor rules that supercharge your coding workflow. Step-by-step tutorial with examples for TypeScript, Python, and React projects.
What Are Cursor Rules?
Cursor rules are configuration files that tell Cursor how to behave when generating and editing code. Think of them as a system prompt for your IDE โ they define coding style, patterns, and constraints that the AI follows every time it writes code for you.
A well-crafted Cursor rule can be the difference between spending 5 minutes fixing AI output and getting production-ready code on the first try.
Why Cursor Rules Matter
Without rules, Cursor uses its default behavior โ which is good but generic. With rules, you get:
- Consistent code style across your entire project
- Framework-specific patterns (Next.js App Router vs Pages, etc.)
- Team conventions enforced automatically
- Fewer iterations โ the AI gets it right the first time
Getting Started: Your First Rule
Cursor rules live in a .cursorrules file at the root of your project. Here's a minimal example:
You are an expert TypeScript developer.
Always use functional components with hooks.
Prefer named exports over default exports.
Use Tailwind CSS for styling.
That's it โ just plain text instructions. But let's go deeper.
Rule Structure Best Practices
1. Start with Role Definition
Tell Cursor what kind of expert it should be:
You are a senior full-stack developer specializing in Next.js 14,
TypeScript, and PostgreSQL. You write clean, maintainable code
with comprehensive error handling.
2. Define Technology Stack
Be explicit about versions and tools:
Tech Stack:
- Next.js 14 with App Router (NOT Pages Router)
- TypeScript 5+ with strict mode
- Prisma 5 for database
- Tailwind CSS 3
- Zod for validation
3. Set Code Style Rules
Code Style:
- Use 'const' by default, 'let' only when reassignment is needed
- Prefer early returns over nested if/else
- Maximum function length: 30 lines
- Always add JSDoc for exported functions
- Use descriptive variable names (not single letters)
4. Define Error Handling Patterns
Error Handling:
- Always wrap async operations in try/catch
- Return typed error objects, never throw in API routes
- Use Result<T, E> pattern for business logic
- Log errors with context (function name, input params)
5. Add Project-Specific Context
Project Context:
- This is a SaaS marketplace for AI skills
- Users can publish, discover, and install AI tool configurations
- Authentication uses NextAuth with GitHub OAuth
- Payments handled through Stripe Connect
Advanced Techniques
Conditional Rules
You can add context-dependent rules:
When writing API routes:
- Always validate input with Zod
- Return proper HTTP status codes
- Include rate limiting headers
When writing React components:
- Use Server Components by default
- Only add "use client" when needed (state, effects, browser APIs)
- Implement loading and error states
Negative Rules (What NOT to Do)
Sometimes it's more effective to say what to avoid:
NEVER:
- Use 'any' type in TypeScript
- Use inline styles (use Tailwind)
- Import from 'react' (it's auto-imported in Next.js)
- Use default exports for components
- Skip error handling in async functions
Example Patterns
Include code snippets as templates:
API Route Pattern:
export async function POST(request: Request) {
try {
const body = await request.json();
const validated = MySchema.parse(body);
const result = await doSomething(validated);
return Response.json(result);
} catch (error) {
if (error instanceof z.ZodError) {
return Response.json({ error: error.issues }, { status: 400 });
}
return Response.json({ error: "Internal error" }, { status: 500 });
}
}
Real-World Example: Complete Cursor Rule
Here's a production-ready Cursor rule for a Next.js SaaS project:
You are a senior TypeScript developer building a SaaS platform.
TECH STACK:
- Next.js 14 (App Router)
- TypeScript 5 (strict)
- Prisma 5 + PostgreSQL
- Tailwind CSS 3
- NextAuth v4
- Stripe
CODE PRINCIPLES:
1. Type safety is non-negotiable โ no 'any', no type assertions
2. Server Components by default, "use client" only when necessary
3. Colocate related code โ components, hooks, types in same directory
4. Prefer composition over inheritance
5. Write self-documenting code; add comments only for "why", not "what"
PATTERNS:
- API routes: Zod validation โ business logic โ response
- Components: Props interface โ component โ exports
- Database: Repository pattern with Prisma
- Forms: Server Actions with useFormState
ERROR HANDLING:
- API: try/catch with typed error responses
- UI: Error boundaries + Suspense boundaries
- Database: Prisma-specific error catching (P2002 for unique, etc.)
TESTING:
- Unit tests for utility functions
- Integration tests for API routes
- Component tests with Testing Library
Sharing and Discovering Rules
Writing great rules takes time. That's where Skill Market comes in โ you can discover and install community-built Cursor rules for any tech stack:
- cursor-typescript-pro โ Battle-tested rules for TypeScript projects
- cursor-react-expert โ React + Next.js optimized rules
- cursor-python-master โ Python best practices for Cursor
Tips for Maintaining Rules
- Version control your
.cursorrulesfile โ it's part of your project - Iterate based on AI output quality โ if Cursor keeps making the same mistake, add a rule
- Keep it focused โ 50-100 lines is the sweet spot; too long and the AI ignores parts
- Share with your team โ everyone benefits from the same rules
- Update regularly โ as your project evolves, so should your rules
Conclusion
Cursor rules are one of the highest-leverage investments you can make in your AI-assisted development workflow. Start with the basics, iterate based on what works, and don't be afraid to get specific.
Ready to skip the setup and get started immediately? Browse pre-built Cursor skills on Skill Market and install them in seconds.
Ready to supercharge your AI workflow?
Browse hundreds of community-built skills for your favorite AI tools.
Browse Skills โ