Bulk Replace
Web and code

How to Rename a Variable or Function Across Multiple Files

Rename a variable, function or class across a whole codebase. When to use IDE refactoring, when text replace is fine, and how to avoid partial matches.

Updated 5 minute read The Bulk Replace team

To rename a variable across multiple files, use your IDE's rename refactoring (F2 in VS Code and Visual Studio) when the language is supported and the files are part of one project, because it understands scope and only changes real references. When that is not available, or the name also appears in templates, config, SQL, docs or other languages, use a whole-word, case-sensitive find and replace such as \boldName\b → newName, and review every match before saving.

This guide explains when each approach is right, the patterns that avoid partial matches, and a checklist for renaming safely across a codebase.

Refactoring rename vs text replace

IDE rename refactoringFind and replace
Understands scopeYes: a local count in one function is separate from anotherNo: every matching word is a match
Works across languagesUsually one language at a timeYes: code, HTML, SQL, YAML, docs
Reaches strings and commentsSometimes, as an optionYes, always
Needs a working projectYes: the code must parse and the language service must loadNo
Works on files outside the projectNoYes

A good rule: use refactoring for local and common names (data, result, count), and text replace for distinctive names that cross boundaries, such as a config key, a CSS class used from JavaScript, an API field name or a product name used in identifiers.

Using the IDE rename

  • VS Code: put the cursor on the name and press F2 (Rename Symbol). This works for languages whose extension provides rename support, such as TypeScript, JavaScript, Python, C# and Go. Shift+F12 (Find All References) first shows where the name is used.
  • Visual Studio: press Ctrl+R, Ctrl+R or F2, with options to include comments and strings.
  • JetBrains IDEs: Shift+F6.

After a refactoring rename, still search the whole repository for the old name. References in HTML templates, SQL, build scripts and documentation are often outside what the language service sees.

Text replace: patterns that avoid partial matches

Whole word and match case, always

Find \buser\b with match case on, replace with account. Without boundaries, user also changes userId, username and superuser. With case-insensitive matching, User (a class) changes as well. Both settings matter.

Naming conventions change the word boundaries

\b treats letters, digits and underscores as word characters, so:

  • \buser\b does not match inside user_id (snake_case), because the underscore joins them into one word. Good.
  • \buser\b does match inside user-id (kebab-case CSS classes and HTML attributes), because the hyphen is a boundary. Use (?<![\w-])user(?![\w-]) to treat hyphens as part of the name.
  • \buser\b does not match inside userId or UserService (camelCase and PascalCase). If you want to rename those compound names too, you need separate rules.

Renaming every form of a name

Renaming a concept usually means several identifiers. For customer to client, a rule list might be:

#Find (regex, match case)ReplaceCatches
1\bCUSTOMER_CLIENT_Constants like CUSTOMER_ID
2CustomerClientCustomer, CustomerService, getCustomer
3customerclientcustomer, customerId, customer_name

Rules 2 and 3 deliberately drop the word boundaries so compound names change too. That is powerful and risky, so this is exactly where reviewing each match matters. For a second opinion on ordering, see finding and replacing multiple words at once.

Only change code, not prose

To rename a property but not the English word, include the syntax around it: \.oldName\b → .newName for property access, or "oldName"\s*: → "newName": for JSON keys.

A safe rename checklist

  1. Commit first. A clean working tree makes the diff easy to review and to undo with git restore ..
  2. Search the old name everywhere, including non-code files, and note the count.
  3. Check the new name is not already in use. If it is, the rename can merge two different things.
  4. Limit file types: source files, templates and config. Exclude node_modules, vendor, build output, lock files and minified bundles.
  5. Review every match, especially in strings, comments and public APIs. A field name in a stored file format or a public API may need to stay the same for compatibility.
  6. Build and run the tests. Compilers and type checkers catch most missed references.
  7. Search for the old name again. What remains should be deliberate.

Places a rename is easy to miss

WhereExampleWhy it is missed
HTML templates{{ customer.name }}, data-customer-idNot seen by the language service
SQL and migrationsSELECT customer_id FROM ...Different language and naming style
Config and environment filesCUSTOMER_API_URL=Upper snake case, outside the code
Strings built at runtime'get' + 'Customer'The full name never appears in the source
Tests and fixturesJSON fixture keys, mock namesOften excluded from searches by habit
DocumentationREADME, API docs, code commentsNot code, so refactoring tools skip it

A case-insensitive search for the bare word, with no boundaries, across every text file in the repository is the quickest way to find these before and after the rename.

Renaming files that carry the name

If the identifier also appears in file names, such as CustomerService.cs or customer-card.component.ts, those files need renaming too, along with the imports that reference them. In Git, use git mv so history follows the file. See replacing text in file names on Windows for bulk renames.

Renames in public code

If other people or programs use the name, such as a public function in a library, a REST API field or a column in a shared database, a rename is a breaking change. Consider keeping the old name as a deprecated alias for a release or two, so callers have time to update.

Doing the text replace with a preview

For cross-language renames, Bulk Replace lets you stack rules like the table above, each with its own whole-word, case-sensitive or regex setting, and run them in one pass over a folder tree filtered to your source extensions. The before/after diff shows every change in every file, so you can untick the match in a public API or a stored format and keep the rest. Bulk Replace can also replace the name in file and folder names in the same run. Every run is backed up, and one click undoes it, which is handy when the build tells you a rule went too far.

Whole-word matching has edge cases of its own, especially with symbols and hyphens. Our guide to find and replace whole word only covers them.

Frequently asked questions

Is find and replace safe for renaming variables?

It is safe for distinctive names with whole-word matching and a preview. For common names, an IDE rename refactoring understands scope and is safer.

How do I avoid renaming parts of other identifiers?

Use whole-word matching or \b boundaries, and match case. That stops user changing userId or currentUser.

What about strings and comments that mention the name?

Text replace changes them too, which is often what you want. Review them in the preview and skip any that should keep the old name.

Get three guides like this every month, plus software update notices.Subscribe →