Introduction
Cursor has revolutionized AI-assisted coding by allowing users to create custom rules that shape how the AI behaves, codes, and responds. These rules, stored in .cursorrules files, enable developers to enforce coding standards, specify project conventions, and create personalized AI behaviors.
This comprehensive guide covers everything about Cursor rules: from basic configuration to advanced patterns, helping you customize Cursor for maximum productivity.
What Are Cursor Rules?
Overview
Cursor rules are instructions that guide AI behavior:
- File-based: Stored in
.cursorrulesin project root - Hierarchical: Can be global, workspace, or project-specific
- Customizable: Define any behavior you want
Why Rules Matter
| Without Rules | With Rules |
|---|---|
| Generic responses | Project-specific context |
| Inconsistent style | Enforced conventions |
| No context awareness | Deep understanding |
| Manual prompting | Automated behavior |
Getting Started
Creating .cursorrules
Create a .cursorrules file in your project root:
touch .cursorrules
Basic Structure
# Project: My SaaS App
# Description: Next.js + TypeScript + Tailwind
## Code Style
- Use TypeScript for everything
- Prefer functional components
- Use Tailwind CSS for styling
## Naming Conventions
- Components: PascalCase
- Hooks: camelCase starting with use
- Utils: camelCase
## Patterns
- Server components by default
- Client components only when needed
- API routes in /app/api
Rule Categories
1. Code Style Rules
## Code Style
- Use 2 spaces for indentation
- No semicolons
- Single quotes for strings
- Trailing commas always
- Prefer const over let
- Use arrow functions
- Destructure when possible
2. Framework-Specific Rules
## React/Next.js
- Use Server Components by default
- Add 'use client' only when needed
- Use Next.js Image component
- Use Next.js Link for navigation
- Prefer async/await over .then()
## TypeScript
- Always define types, never use 'any'
- Use interfaces for objects
- Use type for unions
- Enable strict mode
3. Testing Rules
## Testing
- Write unit tests for utilities
- Write integration tests for components
- Use Vitest
- Test files: *.test.ts
- Mock external APIs
- Coverage minimum: 80%
Advanced Patterns
Environment-Specific Rules
## Development
- Console.log allowed
- Source maps enabled
- Hot reload expected
## Production
- No console.log
- Error boundaries required
- Performance optimized
- Analytics events required
Multi-Stack Projects
## Frontend (React)
- Components in /components
- Pages in /app
- Styles in /styles
## Backend (Node.js)
- Routes in /routes
- Models in /models
- Controllers in /controllers
## Database (PostgreSQL)
- Use Prisma ORM
- Migrations in /migrations
- Seeds in /seeds
Example Rules Files
Next.js Project
# .cursorrules for Next.js Project
## Tech Stack
- Next.js 14 (App Router)
- TypeScript
- Tailwind CSS
- Prisma
- PostgreSQL
## Code Style
- Use TypeScript for everything
- Use 2 spaces for indentation
- Use double quotes for strings
- Add trailing commas
- Use functional components with arrow functions
## Component Structure
- Server Components by default
- Use 'use client' only for hooks and event handlers
- Colocate components with their styles
- Use index files for clean imports
## API Routes
- Create in /app/api
- Use route.ts for handlers
- Validate with Zod
- Return proper status codes
## Database
- Use Prisma client
- Types generated from schema
- Use migrations for changes
- Soft deletes where appropriate
## Testing
- Vitest for unit tests
- Testing Library for components
- Minimum 80% coverage
## Git
- Conventional commits
- PRs required for main
- Squash merges
Python/ML Project
# .cursorrules for Python ML Project
## Tech Stack
- Python 3.11+
- PyTorch
- Pandas
- Scikit-learn
- FastAPI
## Code Style
- PEP 8 compliant
- Type hints required
- Docstrings for all functions
- Use dataclasses for data structures
## ML Pipeline
- Use hydra for config
- Log with wandb
- Version data with DVC
- Use ruff for linting
## API
- FastAPI for endpoints
- Pydantic for validation
- OpenAPI docs enabled
## Testing
- pytest for tests
- fixtures in conftest.py
- Mock external services
Best Practices
Organization
# Well-organized rules file
## 1. Project Overview
- Brief description
- Tech stack
## 2. Code Style
- Formatting rules
## 3. Patterns
- Framework-specific
## 4. Testing
- Test requirements
## 5. Git
- Workflow rules
Keep It Current
# Update rules when:
- Adding new dependencies
- Changing frameworks
- Updating team conventions
- New team members join
Share Across Team
# Team rules in repo
- Commit .cursorrules to git
- Document changes
- Review in PRs
- Sync with team
Troubleshooting
Rules Not Applying
# Check:
1. File named correctly: .cursorrules
2. In project root
3. Valid YAML/markdown syntax
4. No special characters in path
Conflicting Rules
# Resolution:
1. Most specific rule wins
2. Later rules override earlier
3. Document overrides
4. Test after changes
External Resources
Documentation
Examples
- [Awesome Cursor Rules](https://github.com/awesome cursor-rules)
- Cursor Rules Gallery
Conclusion
Cursor rules transform Cursor from a generic AI coding assistant into a project-aware, convention-enforcing pair programmer. By investing time in creating comprehensive rules, you save time on repetitive tasks and ensure consistency across your codebase.
Key takeaways:
- Start simple - Add rules incrementally
- Be specific - Clear rules = better AI behavior
- Keep updated - Rules evolve with your project
- Share with team - Consistency across team
- Test and iterate - Refine based on results
Comments