The Problem
You need to rename a configuration variable, for example fromDATABASE_URL to POSTGRES_DSN, across your entire project. Doing this manually is tedious and error-prone. Using sed or mass find-and-replace can be dangerous if the variable name is a common substring.
Ingredients
- A code project where you need to perform the refactoring.
- ChatCLI in interactive mode.
Step by Step
Delegate the Task to the Agent
Start Agent Mode with a clear and detailed instruction. Be specific about what needs to be done.
Deconstructing the Prompt:
- “across the entire project…”: Defines the scope of the operation.
- “find… and replace…”: Describes the refactoring action.
- “Show me the files… before making any changes”: This is a crucial safety instruction. We are asking the agent to create a plan that allows us to validate the scope before executing the change.
Review the Action Plan
The AI will analyze your request and propose a plan. A good plan for this task will likely have two steps:
Execute and Validate the First Step
Instead of running everything at once, let’s execute only the first command to validate the scope.At the agent prompt, type
1 and press Enter.ChatCLI will run grep -rl "DATABASE_URL" . and display the list of files containing the string.Example Output:Now you can review this list. If it contains an unexpected file (e.g.,
README.md), you know the approach needs to be refined.Edit the Plan for Greater Safety (Optional but recommended)
The Now, the
sed command is powerful but can be risky. Let’s edit command 2 to use a backup extension, creating copies of the original files.At the agent prompt, type e2 (edit command 2). ChatCLI will ask for the new command.sed command will create a .bak file for each modified file, allowing easy rollback if something goes wrong.Execute the Refactoring and Wrap Up
With the plan validated and improved, execute the second command by typing
2.The agent will perform the replacement. After completion, you can verify the changes with git diff and, if everything looks correct, remove the backup files. You can even ask the agent to do this:The refactoring was completed successfully. Now, please create a command to find and remove all .bak files in the project.