Volt
00000000

Guide

How to find and replace across many files on a Mac

Renaming something across a project is one command in Terminal and one mistake away from rewriting files you did not mean to touch. Here is the command, what goes wrong with it, and how to do the same thing where you can see it first.

In Terminal

grep -rl 'oldName' src lists the files that contain it. Piped into xargs sed -i '' 's/oldName/newName/g' it replaces it everywhere — note the empty '' after -i, which the sed on a Mac needs and Linux's does not.

What it does not do is show you anything first. It happily rewrites a binary file, a file in a build folder, a file with a different character set that it then damages, or a file you have open in an editor with unsaved changes — which the editor then saves back over.

In Volt

Find in Project (⇧⌘F) runs the find bar's search — literal or regular expression, case, whole words — over every file of the project, with the results in a list you can click through, each line with the file's name in front of it.

Replace in Project (⇧⌘H) searches first and says how many matches it found in how many files, and only then writes. A file that is open in a window is never written behind its back: the change is made in the window, as an edit you can undo and save. Closed files are written the way Save writes them, so their character set, byte order mark and line endings survive. Binary files are refused.

Only the files you mean. File Filter… takes include and exclude lists in the patterns you already know — *.swift, Sources/, *.{js,ts} — and the replace obeys the same filter as the search. In a Git repository the project is what Git says it is, so .gitignore decides and the build folder is left alone.

Replacements with more to them

  • Regular expressions with $1 in the replacement, and \n, \t for the characters they name.
  • Preserve Case When Replacing: word becomes name, WORD becomes NAME, Word becomes Name.
  • Structural search — code with holes in it: print(:[x]) finds every call however long its argument, and log(:[x]) puts it back renamed.
  • Batch Replace… — a whole list of pairs, the kind a spreadsheet copies as two columns, run over every open document at once with a count first.
  • Find in Open Documents (⌥⌘F) — every window as it is now, unsaved text included.

Questions

How do I find and replace in multiple files on a Mac?

In Terminal, grep -rl finds the files and sed -i '' replaces in them. In Volt, Replace in Project (⇧⌘H) counts the matches and files first, then writes.

Can I limit it to some files?

Yes. File Filter… takes include and exclude patterns such as *.swift or Sources/, and the replace uses the same filter as the search.

What happens to files I have open?

They are changed in their windows, as edits you can undo and save — never written behind your back.

Does it support regular expressions?

Yes, with $1-style groups in the replacement, case matching, whole words, and \n and \t in both fields.

Will it damage files in other encodings?

No. Files are written the way Save writes them, keeping their character set, byte order mark and line endings, and binary files are refused.

Rename something across your project

Volt is in free beta. Download it now — no account, no sign-up — and try it on the file you have in mind. Each build runs for seven days, and every update gives another seven.

Download Volt for Mac

macOS 13 or later, Apple silicon. About 8 MB, signed and notarised. Everything it does · what changed in this build.

Related