Cursor Rules and Habits That Keep Your Codebase Clean

· 7 min read ·
vibe-coding cursor rules workflow

A man in a green sweater giving a thumbs up while a small robot pins one short index card to his monitor, with a bin of crumpled long scrolls beside the desk

Keep one always-on Cursor rule file of about 30 lines, holding only what Cursor cannot work out from your code: commands to run, conventions to follow, places not to touch. Enforce everything else with a linter, tests and small commits, because a rule is a request and a check is a fact.

What does a messy Cursor codebase look like?

Three weeks in, there are two date helpers, components styled three ways, and a package you never asked for. Every prompt got a decent answer, and none knew about the others.

The cause: each new chat starts blank. Rules are the part that survives from chat to chat. When a rule applies, its contents are included at the start of the model context.

Should the rules file be long?

No, and the evidence is better than most advice on this topic. A February 2026 paper, Evaluating AGENTS.md, tested coding agents with and without repository context files on real tasks. Three findings matter here:

  • Context files did not improve task success in general, and raised cost by over 20% on average.
  • The agents followed the instructions, so the extra cost came from more exploring and testing.
  • Repository overviews (“src holds the components”) did not help, since agents find that themselves. Context files earn their place for non-standard practices, such as a house convention.

One more finding: hand-written context files beat LLM-generated ones by about 7% on average (the paper’s introduction states this), so do not let the agent rewrite your rules file after every session.

Caveat: it used Python projects and Claude Code, Codex and Qwen Code, not Cursor.

Cursor’s docs say to keep rules under 500 lines. Read that as a ceiling. Cursor’s agent best practices post says to add a rule when the agent repeats the same mistake, not before.

A starter rule to paste

Save this as .cursor/rules/project.mdc and swap in your own stack:

---
description: Core conventions for this project
alwaysApply: true
---
# Commands
- Typecheck: `npm run typecheck`. Tests: `npm test`. Run both before you say a task is done.
# Conventions
- Before writing a helper, search `src/lib/` for one that already does the job. Reuse it.
- Styling is Tailwind only. Do not add CSS files.
- Do not add a dependency without asking me first. Tell me what it is for and what in package.json comes closest.
- Database changes are new files in `supabase/migrations/`. Never edit an existing migration.
- Do not edit existing tests to make them pass. Tell me which behavior changed.
# Do not touch
- `.env*`, `src/generated/`

The test for each line: would a new teammate need this explained, and could they not learn it from the code? If a linter can enforce it, use the linter (below).

Which of the four rule types should you use?

Cursor lets a rule be applied four ways, set by the alwaysApply, description and globs fields:

TypeFrontmatterWhen it loads
AlwaysalwaysApply: trueEvery chat
Auto-attachedglobs set, alwaysApply: falseWhen a matching file is in context
Agent-chosendescription onlyWhen the agent decides it is relevant
Manualnone of the threeWhen you type @rule-name

Use Always for the five to ten lines above. A glob is a file pattern: **/*.ts means every TypeScript file. Put anything specific to one area in an auto-attached or manual rule so it does not cost context in every chat.

Why isn’t Cursor following my rule?

Check three things.

1. The file extension. Project rules must end in .mdc. Cursor’s docs say a plain .md file in .cursor/rules is ignored by the rules system.

2. Whether a glob rule could have fired. An auto-attached rule loads when a matching file is in context, so a fresh chat with no .ts file involved has not loaded your TypeScript rule yet. A May 2026 forum thread reports glob rules never applying, and replies disagree about the frontmatter syntax. It is not settled, so check the syntax against your own setup.

3. A canary line. Add End every reply with the word "loaded" to the rule, then ask for a tiny edit in a matching file. If the word never shows up, the rule is not loading. If it does, the rule needs clearer wording. It proves loading, not obedience.

Which habits matter more than the rules?

  1. Plan first for anything bigger than one file. Press Shift+Tab in the agent input for Plan Mode, which writes a plan you can edit before any code changes. When the result is wrong, Cursor suggests reverting and tightening the plan instead of patching with follow-ups. For a brand new app, the same idea one level up is spec, slices, then prompts.
  2. One task per chat. Start a new chat when you switch tasks or when the agent repeats a mistake.
  3. Commit before each prompt that matters. A commit is the only undo that always works. See why one AI fix can break two things.
  4. Give it a check that can fail. Typecheck, tests, lint. Cursor’s TDD workflow tells the agent to write tests first, confirm they fail, and then not to modify the tests while making them pass.
  5. Read the diff.

How do you turn a rule into a check?

A linter scans your code against rules and fails when one is broken. Say your rule is “do not use moment, we use date-fns”. A rule in a prompt can get lost. An ESLint rule fails the build. Real projects use TypeScript, so install a parser: npm i -D eslint typescript typescript-eslint, and add "type": "module" to package.json. In a scratch project (ESLint 10.16.0, typescript-eslint 8.74.0), this config fails a file that imports moment with 'moment' import is restricted from being used. Use date-fns:

eslint.config.js
import tseslint from "typescript-eslint";
export default [
tseslint.configs.base,
{
files: ["src/**/*.{ts,tsx}"],
rules: {
"no-restricted-imports": ["error", {
paths: [{ name: "moment", message: "Use date-fns." }],
}],
// import rules do not see require() or await import()
"no-restricted-syntax": ["error",
{ selector: "CallExpression[callee.name='require'][arguments.0.value='moment']",
message: "'moment' require is restricted. Use date-fns." },
{ selector: "ImportExpression[source.value='moment']",
message: "'moment' dynamic import is restricted. Use date-fns." },
],
},
},
];

Two details matter. The first config line lets ESLint read type annotations; without it every typed .ts file fails with Parsing error: Unexpected token :. And the glob covers .tsx, where React components live; a .ts-only glob let a moment import in a .tsx file pass.

Test it with six files (the tests check behavior, which files fail, not how the config is written):

src/c.ts import m from "moment"; const when: string = m().format();
src/d.tsx import m from "moment"; export const D = () => <p>{m().format()}</p>;
src/e.ts const m = require("moment"); const when: string = m().format();
src/f.ts export async function f() { const m = await import("moment"); return m.default().format(); }
src/ok.ts import { format } from "date-fns"; const when: string = format(new Date(), "yyyy");
src/ok.tsx import { format } from "date-fns"; export const Ok = () => <p>{format(new Date(), "yyyy")}</p>;

Run npx eslint on each. Real output from the scratch run:

src/c.ts error 'moment' import is restricted from being used. Use date-fns exit=1
src/d.tsx error 'moment' import is restricted from being used. Use date-fns exit=1
src/e.ts error 'moment' require is restricted. Use date-fns exit=1
src/f.ts error 'moment' dynamic import is restricted. Use date-fns exit=1
src/ok.ts (no output) exit=0
src/ok.tsx (no output) exit=0

ok.ts and ok.tsx are the controls: clean typed files pass, so the four failures are the rule firing, not the parser breaking. The first version of this config (no parser, .ts-only glob) failed c.ts and ok.ts with a parsing error and passed d.tsx. The second version still passed e.ts and f.ts, because no-restricted-imports does not see require() or await import(), which the no-restricted-syntax rule now covers.

For the drift you cannot lint, such as files and exports nothing uses any more, Knip (npx knip) finds unused files, exports and dependencies. In a scratch run, with no entry point configured it flagged a file that index.ts imports, so set entry in a knip.json before you trust its list.

Can a rule keep secrets out of the AI’s reach?

No. Cursor’s docs say .cursorignore blocks ignored files from the agent and @ mentions, but terminal and MCP tools cannot be blocked by it, and complete protection is not guaranteed. A rule saying “never read .env” is only a request. Keep real secrets out of the project folder: how keys leak from AI-built apps.

If you use Claude Code too, its first week covers deny rules and hooks, which enforce what a rules file can only ask.

A prompt to check for drift

Audit this repo for drift. Do not change any code. List: helpers that duplicate each other, components styled differently from the majority, dependencies in package.json that no file imports, and files nothing imports. For each, name the files and say which copy you would keep and why. Ask me before changing anything.

What an engineer checks by hand afterward: run git status --short and git diff --stat HEAD after every agent session. The second command skips brand-new files, which is why you need the first. If you asked for a small change and 14 files moved, stop and read why. Before launch, use the launch checklist.

The Get AI Native ebook goes deeper on this. It is free, and the expected launch is around Christmas. Join the waitlist on the homepage.

Comments