How to Rename Symbols and Refactor Code Safely in VS Code

Rename variables, functions, and classes across your entire project. Use F2, Code Actions, the Refactor Preview panel, and automated Code Actions on save.

5 min read

VS Code can rename a variable, function, or class across your entire project in one step. It also offers structured refactorings like Extract Method and Extract Variable that transform code without changing behavior.

These features require a language service. JavaScript and TypeScript have full support built in. For Python, Go, Rust, Java, C#, and C++, install the official language extension from the Marketplace.

Rename a symbol with F2

Place your cursor on a variable, function, class, or any named symbol. Press F2 (Windows, Linux, and macOS). Type the new name and press Enter.

VS Code renames the symbol everywhere it is used: in the same file, in other files, in import statements, and in comments and strings if the language service supports it. The change happens instantly. Press Ctrl+Z (Cmd+Z) to undo if needed.

If the language service cannot resolve all references, or if you are editing a plain text file, the rename command falls back to a local text search within the current file.

Rename a file and update imports

When you rename a file in the Explorer, VS Code can automatically update import statements that reference it. A prompt appears asking whether to update imports. Select Yes and every file that imports the renamed module updates its path.

This avoids broken imports when you reorganize a project. If the prompt does not appear, check that your language extension supports this feature.

Quick Fixes with Ctrl+.

When there is an error (red squiggle) under your cursor or when a language service detects an improvement opportunity, a lightbulb icon appears in the gutter. Press Ctrl+. (Windows/Linux) or Cmd+. (macOS) to open the Quick Fix menu.

Quick Fixes include:

  • Error corrections. Fix a typo, add a missing import, or convert a type.
  • Refactorings. Extract a method, extract a variable, or convert between named and default exports.
  • Source actions. Organize imports, auto-fix lint violations, or add missing properties.

Select an action with the arrow keys and press Enter. The change is applied immediately.

To see only refactoring actions without error fixes, press Ctrl+Shift+R (Windows/Linux) or Cmd+Shift+R (macOS) instead. This opens the Refactor menu directly.

Preview refactoring changes

Before applying a refactoring that touches multiple files, you can preview the changes.

Hover over a refactoring in the Code Actions menu and press Cmd+Enter (macOS) or Ctrl+Enter (Windows/Linux). The Refactor Preview panel opens on the right.

The panel lists every file that would change. Click a file to see a diff view of the proposed edits. Deselect specific changes you want to skip. Press Accept to apply the selected changes, or Discard to cancel.

This is useful when Extract Method or Extract Variable would affect more code than you expected.

Code Actions on save

You can configure VS Code to run Code Actions automatically every time you save. Open your settings.json and add:

jsonjson
"editor.codeActionsOnSave": {
  "source.organizeImports": "explicit",
  "source.fixAll": "explicit"
}

With this configuration, saving a file organizes imports and applies all available auto-fixes from linters and formatters. Common Code Actions on save include:

  • Organize imports. Remove unused imports and sort the remaining ones.
  • Fix all. Run every available lint auto-fix.
  • Add missing imports. Auto-import symbols used in the file.

You can run Code Actions on explicit save only (when you press Ctrl+S) or on all saves including Auto Save. Use "explicit" (default), "always", or "never" for each action.

Keyboard shortcuts for specific refactorings

If you use a particular refactoring often, bind it to a keyboard shortcut. Open keybindings.json and add an entry with the editor.action.codeAction command:

jsonjson
{
  "key": "ctrl+shift+e",
  "command": "editor.action.codeAction",
  "args": {
    "kind": "refactor.extract.function",
    "preferred": true
  }
}

This triggers the preferred Extract function refactoring for the current selection. Set "apply": "first" to apply it automatically without showing a menu. Set "apply": "ifSingle" to show a menu only when multiple options exist.

What languages support refactoring

LanguageRename (F2)Extract MethodExtract VariableCode Actions
JavaScript / TypeScriptYes (built-in)YesYesYes
Python (Microsoft extension)YesYesYesYes
Go (Go Team at Google)YesYesYesYes
C# (C# Dev Kit)YesYesYesYes
Java (Red Hat)YesYesYesYes
Rust (rust-analyzer)YesYesYesYes

If your language is not listed, open the Extensions view and search for its official extension. Refactoring support varies by extension. Browse the extension's page on the VS Code Marketplace to see which features it supports.

When rename does not work

If F2 does nothing, your language extension may not be active. Check the Status Bar language indicator. If it says "Plain Text," click it and select the correct language. If the correct language is active and rename still fails, restart VS Code to reload the language service.

For changes that rename cannot automate, use the find and replace with regex or multiple cursors to make the same edit across many locations. For more productivity workflows, see the keyboard shortcuts cheat sheet.

Rune AI

Rune AI

Key Insights

  • Press F2 to rename a symbol. Type the new name and press Enter. Every usage across all files updates.
  • Press Ctrl+. (Cmd+.) to open Quick Fixes. The lightbulb in the gutter means a Code Action is available.
  • Press Ctrl+Shift+R (Cmd+Shift+R) for refactoring-only actions: Extract Method, Extract Variable, and more.
  • Hover over a refactoring and press Cmd+Enter to open the Refactor Preview panel and see proposed changes before applying.
  • Configure editor.codeActionsOnSave to automatically organize imports or fix lint issues every time you save.
RunePowered by Rune AI

Frequently Asked Questions

Does rename work in all programming languages?

Rename (F2) works wherever a language extension provides symbol-level support. JavaScript and TypeScript have full rename support built in. For Python, Go, Rust, Java, C#, and C++, install the official language extension. If no language service is available, you can still use Ctrl+D multi-cursor to rename within a file.

What is the difference between Quick Fix and Refactor?

Quick Fix (Ctrl+.) shows both error fixes and refactoring options together. Use it when the lightbulb appears in the gutter. Refactor (Ctrl+Shift+R) shows only refactoring actions like Extract Method and Extract Variable, without error fixes. Use it when you want to restructure code that has no errors.

Can I undo a refactoring if it goes wrong?

Yes. All rename and refactoring operations are normal edits. Press Ctrl+Z (Cmd+Z) to undo. The Refactor Preview panel also lets you deselect specific changes before applying them, so you can apply only the parts you want.

Conclusion

Rename symbols with F2 and VS Code updates every reference across your entire project. Use Ctrl+. to open Quick Fixes for error corrections and refactorings. Preview changes with Cmd+Enter before applying. Configure Code Actions on save to automatically organize imports and fix issues. For more productivity editing tips, see the guide on using multiple cursors and the keyboard shortcuts cheat sheet.