Last updated 13 October 2026
Force Claude Code to follow a specific indentation style
The frustration of inconsistent indentation in generated code
You run the claude command to start a new session. You provide a prompt for a complex feature or a simple bug fix. The agent performs the task with high logical accuracy but fails on the visual details. You open your editor and find that the new code uses two spaces while your entire project uses four spaces. Or perhaps you have a strict policy for tabs and the agent provides spaces instead. This creates immediate friction in your workflow because you must now manually reformat the code or run a separate tool to fix the style. It makes the integration of the agent feel less like a teammate and more like an external contractor who does not follow the house rules.
Claude Code relies on the training data of its underlying model to make decisions about formatting. By default it might choose the most common style found in open source repositories which might not match your specific project. It does not automatically scan your entire repository to infer a style guide unless you explicitly tell it to do so. This leads to a mix of indentation styles within the same file which can break builds in languages like Python or trigger linting errors in others. You need a way to make your preferences permanent so that every session respects your choice of tabs or spaces without you having to repeat yourself in every prompt.
Establishing a source of truth with CLAUDE.md
The most effective way to force a specific indentation style is to use a CLAUDE.md file. This file acts as the primary instruction manual for the agent within your repository. You should place this file in the root directory of your project. When you start a session using the claude command the agent immediately reads this file to understand the project context. To set your indentation rules create a section in this file titled Code Style or Formatting. Be very explicit in your language. Write a sentence like Use four spaces for all indentation and do not use tabs. If you prefer tabs write Always use tabs for indentation and never use spaces.
The agent treats the instructions in CLAUDE.md with high priority. It uses these rules whenever it calls the Edit or Write tools to modify your files. By defining the style here you ensure that the instructions persist across different sessions. You do not need to remind the agent about your spacing preferences in every new conversation because the file is part of the persistent project state. This is the first thing the agent checks before it attempts to write a single line of code. It effectively overrides the default behavior of the model with your project specific requirements.
Handling mixed styles with subdirectory instructions
In some large projects you might have different indentation requirements for different parts of the tree. For example your frontend code in a web folder might use two spaces while your backend code in a server folder uses four spaces. Claude Code supports a hierarchical instruction system to handle this. You can place a secondary CLAUDE.md file inside a specific subdirectory. The agent looks for the instructions closest to the file it is currently working on. If it is editing a file in the web folder it will follow the rules in web/CLAUDE.md before looking at the root version.
This allows you to maintain a granular level of control over code style. In your subdirectory files you only need to include the rules that differ from the root. The agent is smart enough to combine these instructions. If the root file says to use semicolons but the subdirectory file says to use two spaces the agent will use both rules when working in that folder. This prevents the agent from making mistakes when switching context between different technology stacks within the same repository. It ensures that the Write tool always produces code that blends into its immediate surroundings.
Setting global defaults for every project
If you have a personal preference that you want to apply to every project you work on you can use a global instruction file. This file lives at ~/.claude/CLAUDE.md in your home directory. This is particularly useful if you are a developer who always uses a specific style regardless of the project. The agent reads this global file in every session no matter which folder you are in. It is a powerful way to set a baseline for how the agent should behave across your entire machine. You can put your preferred spacing and bracket styles here to save time on every new project you start.
Keep in mind that project specific files will usually take precedence if there is a conflict. If your global file says to use tabs but a project specific CLAUDE.md says to use spaces the agent will follow the project file. This is the intended behavior because it allows you to adapt to team standards while keeping your personal preferences as the default for your own work. Using the global file ensures that even when you are experimenting in a new empty folder the agent still knows how you like your code formatted from the very first prompt.
Configuring tool behavior and permissions
Claude Code uses specific tools to interact with your filesystem. The Edit tool is used for targeted changes and the Write tool is used for creating new files or overwriting existing ones. These tools are governed by your settings. You can view your current configuration by using the /config command inside a session. This shows you how the agent is set up to handle permissions and tool usage. If you find that the agent is frequently making formatting mistakes you should check if your permission modes are interfering with its ability to read your instruction files correctly.
You can cycle through permission modes using the Shift+Tab shortcut. The modes include default and acceptEdits among others. In acceptEdits mode the agent has more freedom to apply changes which can be helpful once you have established your style rules in CLAUDE.md. If the agent knows it has permission to edit and it has clear instructions on style it will be more consistent. You can also look at the .claude/settings.json file in your project to see if any specific tool permissions are restricted. Ensuring the agent has full access to read your configuration files is key to maintaining style consistency.
Managing multiple projects with adhdTerminal
When you are working on several projects at once the differences in code style can become confusing. One project might use tabs and another might use two spaces. Keeping track of which rules apply to which terminal window is a mental burden. This is where adhdTerminal provides a concrete benefit. It allows you to run many Claude Code sessions side by side as tiles on a single wall. Each tile has a name and a color which helps you visually separate your projects. You can see at a glance which session belongs to the tabs project and which belongs to the spaces project.
Because adhdTerminal tiles reopen in the same place after a restart and resume the same session you do not lose the context of your style rules. You can monitor the last exchanges of each conversation on the tile itself to see if the agent is adhering to the indentation rules you set in CLAUDE.md. If a tile shows that the agent has started using the wrong spacing you can quickly jump into that session and correct it. This visual overview makes it much easier to manage the output of the agent across a diverse set of repositories without getting your formatting styles mixed up.
Using hook events to reinforce style
Claude Code supports hook events that trigger at specific points in the conversation. These include events like UserPromptSubmit and PreToolUse. You can use these hooks in your settings.json to reinforce your style rules. While you cannot run complex external scripts inside the agent itself you can define instructions that the agent should keep in mind during these events. For example you can use a hook to remind the agent to check the indentation rules every time you submit a prompt. This acts as a constant nudge to ensure the agent does not drift from the established project style.
To set this up you would modify the hooks section in your .claude/settings.json file. Mentioning your indentation preference in a PreToolUse hook can be very effective because it places the instruction right before the agent uses the Edit tool. This minimizes the chance that the model will forget the rules during a long or complex task. It creates a more robust environment where the agent is constantly reminded of the constraints it must work within. This is especially useful in very long sessions where the initial context of the CLAUDE.md file might be less prominent in the agent memory.
Recovering from formatting errors and context drift
Even with perfect instructions the agent might occasionally make a mistake. This usually happens in long sessions as the conversation history fills up the context window. If you see the agent using the wrong indentation you can use the Escape key to interrupt its current task. If it has already finished the task and the formatting is wrong you can press the Escape key twice. This opens a list of earlier messages allowing you to go back to a point in time before the formatting error occurred. This is often faster than trying to ask the agent to fix its own mistake.
Another useful command is /compact. This command summarizes the session so far and frees up space in the context window. When the agent summarizes the session it retains the most important information including the instructions from your CLAUDE.md file. This can help clear out the confusion of recent formatting errors and refocus the agent on your core style rules. If the session has become too messy you can use the /clear command. This starts the session over in the same folder giving you a fresh start while still respecting the rules you have defined in your project files.
Verifying style with automated tools
The best way to ensure that the agent has followed your indentation rules is to use the tools you already have in your project. You can ask the agent to run a linter or a formatter using the Bash tool. For example you can type a prompt like Run the linter and fix any indentation issues it finds. Because the agent can execute shell commands it can interact with tools like Prettier or ESlint to verify its own work. This creates a closed loop where the agent writes code and then immediately checks it against your actual project standards.
You can also use the /status command to see if the agent is currently aware of any issues. By combining the proactive instructions in CLAUDE.md with the reactive checks of a linter you can achieve a very high level of consistency. The agent is very good at following instructions that involve running a specific command and then acting on the output. If the linter returns a list of formatting errors the agent will use the Edit tool to correct them. This final step ensures that even if the model had a slight lapse in following the style guide the final committed code will be perfect.
Questions people ask
Does Claude Code automatically read my .editorconfig file?
No, Claude Code does not natively parse .editorconfig files to determine its own output style. You must explicitly state your indentation preferences in a CLAUDE.md file or instruct the agent to run your project formatter after it makes changes.
How do I fix a file that already has mixed indentation from the agent?
You can use the Bash tool to run your project's formatting command, or ask the agent directly to refactor the file to match the style defined in your CLAUDE.md. If the error just happened, pressing Escape twice to go back to a previous message is the cleanest way to retry.
Can I set different indentation rules for specific file extensions?
Yes, you can specify this in your CLAUDE.md file. Write clear instructions such as Use four spaces for Python files and tabs for Go files, and the agent will apply these rules based on the extension of the file it is editing.
Read next
- Add build instructions to your CLAUDE.md fileImprove project context by adding specific build commands and environment requirements to your CLAUDE.md file for the AI to follow.
- Add common error fixes to CLAUDE.mdImprove project memory by adding a section for common errors and their fixes to your CLAUDE.md configuration file.
- Add custom shell aliases for Claude CodeSpeed up your workflow by creating shell aliases for common Claude Code invocations and project-specific startup commands.
- Add test commands to a CLAUDE.md fileUnderstand how to define specific test runners and environment flags in your CLAUDE.md file so the tool runs the correct test suite.