181

I have this simple situation. I'd like to refactor the name of the role variable:

Enter image description here

It looks like Visual Studio Code is smart enough to know that "roles" in the URL should not be touched.

I just want to refactor the name of the variable in a single file, in a single scope, not the whole file and definitely not multiple files!

If I use Ctrl + H, it will bring me to a menu which by default refactors the name in multiple files or a whole single file, but I just want to refactor the name in a single function scope!

6 Answers 6

276

Use rename symbol instead of the standard find/replace. Rename is bound to F2 by default.

Rename symbol will know to only touch the local roles references in your example. Visual Studio Code ships with rename support for JavaScript and TypeScript. Other languages may require installing a language extension.

Sign up to request clarification or add additional context in comments.

9 Comments

man, is there a way to use something easier than the f2 key? that's really difficult to use. Why are there not more right-click options?
Right click on the symbol. It should be there. You can also rebind this to whatever keyboard shortcut you want
In my case, is CTRL + F2. I'm currently using version 1.30.2
Note that "Change All Occurences" is different from "Rename Symbol". "Change All Occurences" seems to be limited to the current document, while "Rename Symbol" propagates through the project. In addition to that, "Change All Occurences" will also happily rename properties of different objects, if they are named the same (e.g. if you have two objects a.SomeValue:string and b.SomeValue:number and change the SomeValue property of a with ctrl+F2, it will also rename the property of b, even if they are different types. "Rename Symbol" will only rename the property of the type.
Rename Symbol uses language smarts to rename a symbol in all places it is referenced. Change All Occurrences is text operation that renames every occurrence of the selected text in a file; it's the same as pressing cmd+D a bunch to create multi-cursors for the selected text
|
55

Use Rename Symbol. It is defined by default with Ctrl + F2.

However, keep in mind that the variable, definition, function, etc., that you're changing, will be changed in the file itself, not only the scope. Visual Studio Code for now doesn't have an implementation for renaming a variable in a scope (for example, a variable inside a function). So keep that in mind.

Visual Studio Code - change all occurrences

4 Comments

No idea if it was changed after this answer was posted but there're currently two different commands: Rename Symbol (F2) and Change All Occurrences (Ctrl+F2). The smart one if the first one as far as I know.
Just to add, be careful when using this, I just used it and it changed every instance throughout a whole project not just the file I was working on and vscode could not reverse it, it just came up with an error saying refactor failed. Thank god for Git is all I can say.
@ÁlvaroGonzález Ctrl+F2 seems at least somewhat smart too. $true does not get selected when I right-click on letter t in $t in a .ps1 file and Change All Occurrences.
this burned me badly. I need local scope changes only.
45

For macOS users: Use Fn + + F2 to rename a variable inside a code block.

1 Comment

For me, just Fn + F2
9

To open your user and workspace settings, use the following Visual Studio Code menu command:

On Windows/Linux: Menu FilePreferencesSettings. On macOS: CodePreferencesSettings.

You can also open the Settings editor from the Command Palette (Ctrl + Shift + P) with Preferences, open Settings or use the keyboard shortcut (Ctrl + ,).

Enter image description here

In the search bar, enter keybindings.json, and add this below code:

{
  "command": "editor.action.changeAll",
  "key": "ctrl+f2",
  "when": "editorTextFocus && !editorReadonly"
}

and

{
  "command": "editor.action.rename",
  "key": "f2",
  "when": "editorHasRenameProvider && editorTextFocus && !editorReadonly"
}

In the keybindings.

F2 appears to work across all files, and Ctrl + F2 in the current file only.

4 Comments

do you mean to say keybindings.json instead of settings.json?
the command editor.action.changeAll will do this based on text values where editor.action.rename will do this based on reference. That's why, if you export/import a variable/function name, it'll rename across files. Be aware of this! Also, if you want to rename all references, and not use aliases (like originalName as newName) you can change this in settings.json: "javascript.preferences.useAliasesForRenames": false
On linux XFCE4 ctrl+f2 change the workspace (virtual desktop) .
I got to settings and searched for keybindings.json, but now what? I just see several checkboxes. Please clarify for us noobs where to add the code.
6
  1. select the name with F2

  2. type the new name

  3. press ENTER

    documentation

Comments

4

Select the variable name and press Ctrl + Shift + L. Now type the new variable name and press ESC. This will replace all occurrences simultaneously.

2 Comments

Any idea how 'L' relates to renaming an variable? Just to make an association to remember the shortcut...
"L" for List of selections? Or maybe aLL occurrences? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.