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:
"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:
{
"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
| Language | Rename (F2) | Extract Method | Extract Variable | Code Actions |
|---|---|---|---|---|
| JavaScript / TypeScript | Yes (built-in) | Yes | Yes | Yes |
| Python (Microsoft extension) | Yes | Yes | Yes | Yes |
| Go (Go Team at Google) | Yes | Yes | Yes | Yes |
| C# (C# Dev Kit) | Yes | Yes | Yes | Yes |
| Java (Red Hat) | Yes | Yes | Yes | Yes |
| Rust (rust-analyzer) | Yes | Yes | Yes | Yes |
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
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.
Frequently Asked Questions
Does rename work in all programming languages?
What is the difference between Quick Fix and Refactor?
Can I undo a refactoring if it goes wrong?
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.
More in this topic
How to Use VS Code with WSL 2 on Windows
Run VS Code connected to Windows Subsystem for Linux so you can develop in a full Linux environment with native tools, terminals, and debugging, all from Windows.
20 Best VS Code Extensions for Web Developers in 2026
Twenty carefully chosen VS Code extensions every web developer should know. Covers formatting, linting, frameworks, debugging, Git, and developer experience.
How to Install, Disable, Update, and Uninstall VS Code Extensions
Learn how to install, disable, update, and uninstall VS Code extensions from the Marketplace and the command line. Step-by-step instructions for every action.