Skip to main content
Moxie Docs
All guides

Cursor guide

How to write a .cursorrules file

A .cursorrules file tells the Cursor AI editor exactly how to behave in your project - build commands, coding conventions, database schemas, and style choices. Here is the best way to write, structure, and maintain one without it drifting out of date.

Definition: A .cursorrules file is a plain-text file at the root of a project that gives the Cursor AI editor standing instructions for that project: the tech stack, coding conventions, folder layout, and the commands to run. It is Cursor's original rules format. Cursor's current documentation describes project rules as `.mdc` files in a `.cursor/rules` folder and also reads AGENTS.md, so the same content works in any of the three.

Last reviewed: November 2026

Need living codebase context for Claude Code or Cursor?

Moxie Docs indexes your codebase and serves verified conventions and docs over MCP. Free on 1 repo.

Connect 1 repo free

What is a .cursorrules file?

A .cursorrules file is a project-level instruction file placed at the root of your workspace. It has no required syntax: you write plain sentences and Markdown lists, and Cursor adds them to the context of its AI chat, inline edits (Cmd+K / Ctrl+K), and agent sessions in that project.

It acts as the 'source of truth' for the editor's AI, ensuring it adheres to your preferred coding standards, framework versions, and project structures instead of guessing based on general training data.

A basic .cursorrules template for a beginner HTML/CSS project

If you are starting out, keep the file short. Say what the project is, how you want the code written, and what the AI should leave alone. Save this as .cursorrules in the top folder of your project and change the details to match it:

MARKDOWN
# Project
A small static website built with plain HTML and CSS. No frameworks, no build step.

# Files
- index.html is the home page. Other pages sit next to it.
- css/styles.css holds all styles. Do not use inline styles.
- images/ holds pictures.

# HTML rules
- Use semantic tags: header, nav, main, section, footer.
- Every img needs a descriptive alt attribute.
- Indent with 2 spaces.

# CSS rules
- Use class selectors, not IDs, for styling.
- Write mobile-first: base styles first, then min-width media queries.
- Put colors and spacing in CSS custom properties on :root.

# How to help me
- I am a beginner. Explain what each change does in one or two sentences.
- Do not add JavaScript, Bootstrap, or Tailwind unless I ask.
- Change only the files I mention.

The last section matters as much as the code rules. Telling the AI your experience level and what not to add stops it from rewriting a simple page into a framework project.

A .cursorrules template for an application codebase

For a team project, replace the beginner notes with the facts an AI gets wrong when it guesses: exact versions, where things live, and the commands that verify a change.

MARKDOWN
# Stack
- Next.js 16 (App Router), React 19, TypeScript in strict mode
- PostgreSQL through Drizzle ORM

# Conventions
- Use absolute imports from @/ and `import type` for types.
- Server components by default. Add the use client directive only for interactivity.
- Return typed errors from server actions. Do not throw across module boundaries.

# Layout
- src/app: routes. src/components: shared UI. src/lib: business logic.
- Tests sit next to source as *.test.ts.

# Commands
- Install: npm install
- Test: npm test
- Lint and typecheck: npm run lint && npm run typecheck

# Do not
- Do not edit files in src/generated/.
- Do not add dependencies without asking.

You can draft your own with the free .cursorrules generator, then trim it to the rules that are true today.

.cursorrules vs .cursor/rules vs AGENTS.md

Cursor has since added a second format. Its rules documentation now describes project rules as .mdc files inside a .cursor/rules folder, each with optional frontmatter (description, globs, alwaysApply) that controls whether the rule applies to every session, only to matching files, or when the agent decides it is relevant. The same page says Cursor reads AGENTS.md in the project root and in subdirectories. It no longer mentions .cursorrules, so treat the single root file as the legacy format.

For a new project, put the rules in .cursor/rules/*.mdc when you want per-folder or per-file-type rules, or in AGENTS.md when other agents such as Codex or Claude Code work in the same repository. The templates above work in either: paste the content into the new file, and in an .mdc rule set alwaysApply: true in the frontmatter if it should load in every session. If you keep an existing .cursorrules file, make sure it does not disagree with the newer files.

The ideal structure for a .cursorrules file

A well-structured .cursorrules file is split into logical sections so the model can parse and follow the rules efficiently. We recommend organizing your instructions under these four pillars:

  • System & Tech Stack: List framework versions (e.g. Next.js 16, React 19) and library dependencies so the AI doesn't write deprecated APIs.
  • Coding Standards: Detail import style preferences (e.g. absolute imports, type imports), state management guidelines, and design system rules.
  • Directory Structure: Map where components, API routes, database schemas, hooks, and tests reside to prevent the AI from creating misplaced files.
  • Verification & Run Commands: Provide exact CLI commands to build, test, and lint. This helps Cursor's terminal agent run tests and fix compilation errors autonomously.

The danger of custom instruction drift

While a .cursorrules file is incredibly powerful, it suffers from a common pitfall: documentation drift. When you upgrade dependencies, refactor folder structures, or add new database tables, the static rules file stays the same.

Over time, the .cursorrules file begins to contradict the actual codebase. When the AI reads outdated rules, it hallucinates legacy patterns, leading to broken imports, compilation errors, and slower developer velocity. Committing to keeping these rules manually sync'd is a chore most fast-moving teams fail to sustain.

Transitioning to a dynamic MCP server

For large codebases, a single static .cursorrules file becomes bloated, eating up the AI's finite context window. The modern approach is to transition from static rules files to a live Model Context Protocol (MCP) server.

Instead of sending a massive text file on every prompt, an MCP server lets Cursor query the codebase's rules and metadata dynamically on demand. This keeps prompts clean, saves token costs, and ensures the AI always has the latest source-of-truth context.

How-to

How to set up a .cursorrules file

  1. 1

    Create the file

    Create a blank text file named .cursorrules directly at the root of your project or monorepo workspace.

  2. 2

    Add project instructions

    Write out your tech stack versions, import preferences, directory mapping, and build/test commands using clear, structured Markdown.

  3. 3

    Test with Cursor

    Open Cursor, start a chat or edit prompt, and verify that the AI is referencing the rules from your .cursorrules file.

Keep rules in sync automatically

Instead of maintaining static rules files by hand, let Moxie Docs manage your codebase context. Moxie Docs indexes your repository, generates conventions automatically, and exposes them to Cursor via a built-in MCP server - ensuring your AI agent always writes code aligned with your latest conventions without manual updates.

FAQ

Frequently asked questions

Quick answers to the questions developers ask about How to write a .cursorrules file.

Get started

Give your agents real codebase context.

Connect a repository and Moxie Docs generates living documentation and serves it to your agents over MCP. Free on 1 repository, no card required.