Batch-editing many files

Hi,
Does anyone know the best way to automate this:

I have around 20 files which all require the same edits. I could open the files one by one, run the same prompt in the sidebar, apply the changes and save each file.

But I’d like this to be automatic.

I tried the interpreter mode in the past, but it wasn’t able to accomplish this. It either missed some files, polluted the chat space, resulting in reaching the context limit, or didn’t apply the changes properly.

Can the new composer do that? If I open all the files that require the same edit, can it automatically process all the open files without me adding each file individually?

Or is there something else that can do that?

I had a scenario this evening where I changed the name of several Node Express session object property names in the file where they are set.

I asked Composer (Ctrl + I) if it could update these names wherever they were referenced in my codebase (and clicked the Codebase button, using GPT4o).

I gave it 7 BEFORE and AFTER values, ie ‘please replace X with Y’.

I asked it twice, and both times it told me how I could do it myself :).

It listed several approaches, including using the inbuilt VS Code/Cursor ‘Replace’ functionality

Slightly perturbed, I followed its advice and learned how to use the inbuilt ‘Replace’ functionality.

The inbuilt ‘Replace’ functionality worked well and was quite educational for me, coming from Sublime Text.

(You put in your Find and Replace values, and then, in the results area, you click on the Red/Green diff so you can see the replacement being suggested, then you click the ‘Replace’ icon - once you get more confident at reviewing the changes in the line diffs you can click the ‘Replace All’ button at the file level).

So Composer is normally completely fine doing multi-file create and edit tasks (which is awesome), but for some reason it didn’t want to do this one.

Perhaps it knew I should learn how to use ‘Replace’ properly, or that its resources could be better used on other types of tasks.

I’ve been thinking about replace, but in my case I have slightly different formatting in files, some of the calls are on different lines and sometimes on the same line. I would need a complex regex that would replace things properly in each case, preserving unique names, line breaks and so on. I’ve tried it once with an llm and it couldn’t provide a proper regex, and I don’t care for figuring it out myself. But I will revisit if maybe I could replace at least some stuff with replace, thanks.

Did you try Composer?

It’s worth giving it a shot.

I’m not sure if there is a limit to how many files can be ‘tagged’ in the prompt area with the # symbol, but you could try that, or you could click the Codebase button which, if I understand it correctly, passes though the whole Codebase as context.

If the refactor is limited to a specific piece of code in all files, maybe clearly delineate that in your prompt and say “look at, and refactor, this specific code and nothing else following these instructions…’ just to keep it on track.

I’ve seen others have good results in programs like Aider by being very clear in their prompt about what the LLM should not do.

1 Like