Why Your AI Coding Assistant Needs Rules
When I first started using Cursor, I was amazed at how it could write code. But I quickly got frustrated when it kept suggesting React class components (I prefer functional components) or wrote Python without type hints (which drives me crazy). That's when I discovered .cursorrules files.
Think of .cursorrules as a way to teach Cursor your coding personality. Instead of constantly correcting the AI or writing the same instructions over and over, you create a file that tells Cursor exactly how you like to code. It's like having a conversation with a new teammate about your team's coding standards, except you only have to do it once.
What Are .cursorrules Files?
A .cursorrules file is a simple text file you place in your project's root directory. Cursor automatically reads this file and uses it to understand your preferences, coding standards, and project-specific requirements.
The file uses natural language — no complex syntax or configuration formats. You literally write instructions like you're talking to a human developer. Here's a basic example:
# Use TypeScript for all new files
# Prefer functional components over class components
# Use const assertions where appropriate
# Include proper error handling in async functionsCreating Your First .cursorrules File
Let me walk you through creating a .cursorrules file step by step. I'll use a React project as an example since that's what I work with most.
First, create the file in your project root:
touch .cursorrules
# Or create it through your editorNow, here's a real .cursorrules file I use for my React projects:
# React/TypeScript Project Rules
## Code Style
- Use TypeScript for all new files
- Prefer functional components with hooks
- Use arrow functions for components
- Include proper TypeScript types for all props
- Use const assertions for objects that won't change
## File Organization
- Components go in src/components/
- Custom hooks go in src/hooks/
- Types go in src/types/
- Keep component files under 200 lines
## Error Handling
- Wrap async operations in try-catch blocks
- Use error boundaries for component errors
- Log errors with context information
## Performance
- Use React.memo for expensive components
- Avoid inline object creation in JSX
- Use useCallback for event handlers passed to childrenFramework-Specific Rules
Different projects need different approaches. When I was working on a Python data science project last month, I created completely different rules:
# Python Data Science Project
## Code Style
- Use type hints for all function parameters and returns
- Follow PEP 8 formatting
- Use dataclasses for structured data
- Include docstrings for all functions
## Data Handling
- Use pandas for data manipulation
- Validate data inputs with assertions
- Handle missing data explicitly
- Use pathlib for file operations
## Jupyter Notebooks
- Keep cells focused on single tasks
- Include markdown explanations before complex operations
- Clear outputs before committingFramework Templates
The Cursor community maintains .cursorrules templates for popular frameworks. Search "cursorrules" + your framework name on GitHub for starting points.
Advanced Configuration Techniques
Once you get comfortable with basic rules, you can get more sophisticated. Here are some advanced techniques I've learned:
Context-aware instructions: You can tell Cursor about your project's specific context.
# Project Context
This is an e-commerce application built with Next.js and Prisma.
We use Stripe for payments and AWS S3 for image storage.
When working with product data:
- Always include price validation
- Handle image uploads with proper error states
- Consider inventory levels in purchase flowsTesting preferences: I always include testing guidelines because consistent test patterns save me hours.
# Testing Standards
- Use Jest and React Testing Library
- Test user interactions, not implementation details
- Mock external API calls
- Include accessibility tests with @testing-library/jest-dom
- Name test files ComponentName.test.tsxCommon Mistakes to Avoid
I made plenty of mistakes when I started writing .cursorrules files. Here are the big ones to avoid:
Being too vague: "Write good code" doesn't help. "Use semantic HTML elements and include alt text for images" does.
Making rules too long: I once wrote a 500-line .cursorrules file. Cursor couldn't keep track of all the instructions. Keep it focused on your most important standards.
Contradictory rules: Don't say "always use semicolons" and then "follow Prettier formatting" if your Prettier config removes semicolons.
Ignoring your team: If you're working with others, your .cursorrules should reflect team decisions, not just personal preferences.
Testing Your Rules
After creating your .cursorrules file, test it! Here's what I do:
1. Ask Cursor to create a new component or function
2. Check if it follows your specified patterns
3. If not, refine your rules to be more specific
For example, when I first wrote "use functional components," Cursor still occasionally suggested class components. I changed it to "Always use functional components with hooks. Never suggest class components." Much clearer.
Quick Test
After updating your .cursorrules file, restart Cursor or reload your workspace to ensure the new rules are loaded.
Maintaining Your Rules Over Time
Your coding preferences will evolve, and so should your .cursorrules files. I review mine every few months or when I start getting frustrated with Cursor's suggestions.
Keep a "rules changelog" comment at the top of your file:
# Last updated: 2024-01-15
# Changes: Added accessibility rules, updated testing preferences
# Next review: 2024-04-15This helps you remember what you changed and why.
Start Simple, Then Expand
Don't try to create the perfect .cursorrules file on day one. Start with your biggest pain points — maybe it's always having to ask for TypeScript, or constantly correcting import styles.
Add 3-5 rules that address your most common frustrations. Use Cursor for a week, then add more rules based on what you find yourself correcting repeatedly.
The goal isn't to control every aspect of how Cursor codes. It's to eliminate the friction that slows you down and ensure consistency with your project's standards. When you get that balance right, Cursor stops feeling like an AI tool and starts feeling like a teammate who actually knows how you like to work.
Want to go deeper?
Check out more tutorials in this category, or explore the full site.