Refactoring to Change a Local Variable Name

What follows is a trick to refactor a local variable, best used if the variable occurs many times within a local scope.

In the screenshot below, I highlighted the local variable dict and then entered Control-Command-T. Notice that each reference to this variable is now highlighted. An important note, you don’t need to necessarily highlight the first instance of the variable for this to work.

Once all the variables are highlighted, make the change to the new name and you’ll see all references updated in unison, see the figure below:

Tap your mouse anywhere outside the variable to complete the change.

  1. It’s very slick, one for the “plus” column for Xcode.

    You can do the same thing with just the mouse if you prefer: select the name, hover over it briefly, and choose “Edit All in Scope” from the little menu that appears next to it.

  2. Didn’t work for me with Xcode 4.0.2. Might be Xcode version or my french keyboard mapping… I mapped rename to ctrl+cmd+T, but that’s the “edit > refactor” function which doesn’t work the same.
    By curiosity, could someone tell me what is the command name for this?
    Anyway, still a time savior, thanks!

    • Vincent, I have not installed Xcode 4, good chance the keyboard mapping is different. The name of the command is “Edit all in scope”

  3. Yes I confirm that it is control-command-E.
    Great tip, that darn arrow contextual is too hard to clic! And you gave me the idea of mapping refactor wich works also with methods, properties… and also change code on related files.

    Thanks!

Comments are closed.