Creating and Editing Files in Claude Code
Claude Code does not just explain code — it writes and changes code directly in your project files. You describe what you want, Claude Code drafts the changes, shows you a preview, asks for your approval, and then writes to the file. Your project stays safe because nothing changes without your confirmation.
How File Creation Works
When you ask Claude Code to create a new file, it follows a short sequence:
[ You describe the file you need ]
|
v
[ Claude Code writes the file content ]
|
v
[ Claude Code shows you what it will create ]
|
v
[ You approve ]
|
v
[ File is saved to your project folder ]
Nothing is written to disk until you say yes.
Creating a New File
Tell Claude Code the file name and what it should contain:
"Create a new file called helpers.py with a function that takes a list of numbers and returns the average."
"Create index.html with a basic HTML5 page structure, a header that says 'Welcome', and a centered paragraph."
"Create a file called constants.js that exports the base API URL and the default timeout value as named constants."
Creating Files with Specific Requirements
You can add detailed requirements in the same prompt:
"Create a Python file called email_validator.py with a function validate_email(address) that checks if the address contains an @ symbol and a dot after it, and returns True or False."
Claude Code writes exactly what you describe — you do not need to know the code yourself to get a working file.
How File Editing Works
Editing an existing file follows the same permission pattern:
[ You describe the change ]
|
v
[ Claude Code reads the current file ]
|
v
[ Claude Code drafts the edit ]
|
v
[ Claude Code shows you the before and after ]
|
v
[ You approve ]
|
v
[ File is updated ]
Common File Editing Prompts
Adding Something New
"Add a function called format_date to utils.py that converts a timestamp into DD/MM/YYYY format."
"Add error handling to the fetch_user function in api.py so it catches network timeouts and returns None instead of crashing."
Changing Existing Code
"In config.py, change the default timeout value from 30 to 60."
"Rename the function process_data to transform_data in pipeline.py and update all the places that call it in the same file."
Removing Something
"Remove the debug print statements from main.py."
"Delete the unused import statements at the top of auth.py."
Restructuring Code
"Move the three helper functions at the bottom of app.py into a new file called app_helpers.py, and import them back into app.py."
Diagram: Before and After an Edit
Claude Code shows you a diff — a comparison of what the file looks like before and after the change. Lines marked with a minus sign are removed, and lines with a plus sign are added.
BEFORE AFTER
-def calculate(x): +def calculate(x, y=1):
- return x * 2 + if y == 0:
+ return 0
+ return x * y
You see exactly what will change. Approve only when you are satisfied.
Editing Multiple Files at Once
Claude Code handles changes that span more than one file:
"Rename the class DatabaseConnection to DBConnection in database.py, and update all files that import it from database.py."
Claude Code finds every affected file, shows you all the planned changes, and waits for one approval before making them all. This prevents the common mistake of renaming something in one place and forgetting to update the rest.
Creating Files from a Template Pattern
Describe a pattern once and Claude Code replicates it:
"I have a file called product_service.py. Create a similar file called order_service.py with the same structure — class, constructor, and placeholder methods for create, read, update, and delete."
Claude Code studies the existing file's pattern and applies it to the new one.
Safe Editing Practices
Review Before You Approve
Always read the diff Claude Code shows before you confirm. It takes five seconds and prevents mistakes from getting into your project.
One Change at a Time for Risky Edits
If you are changing a core file that many other files depend on, ask Claude Code to make one change, verify it works, then move to the next. This makes it easy to pinpoint problems.
Ask Claude Code to Explain the Change
If a diff looks confusing, ask before approving:
"Explain why you are adding the try/except block here before I approve this change."
Creating Boilerplate Files Quickly
Boilerplate means the standard starter code every project needs — setup files, base classes, configuration templates. Claude Code generates these instantly:
"Create a .gitignore file suitable for a Python project with Flask."
"Create a basic Dockerfile for a Node.js application that runs on port 3000."
"Create a README.md for this project based on the files you can see in the folder."
Undoing a Change
If you approved a change and want it reversed, ask Claude Code directly:
"Undo the change you just made to billing.py and restore it to how it was."
Claude Code rewrites the file back to the previous state. For extra safety, use a version control system like Git alongside Claude Code — then you have a full history of every change.
Key Points
- Claude Code shows you a preview of every file change before writing it.
- Nothing is saved to disk without your approval.
- You can create new files, add to existing ones, change code, remove code, and restructure files.
- Multi-file edits are handled in one prompt — Claude Code updates all affected files together.
- Always read the before-and-after diff before you confirm a change.
- Use Git alongside Claude Code to keep a full history of every edit.
