Search code examples
sublimetext3

Re-naming variables in Sublime Text 3


Working in Sublime Text 3, I’d like to re-name some variables in my code.

When I select a variable, all occurrences of it are highlighted. Importantly, Sublime Text is smart enough to only highlight the exact matches, not all occurrences of that string. So, while the cursor is on the variable named i, it is not highlighting the i in my variable points or in the keyword in. Yay!

If I try to re-name the variable i by using multiple selection (Cmd+D), though, it selects all the i’s including the wrong ones. Oh no!

As Sublime Text seems to be aware of the exact matches, how can I select all occurences of the variable, in order to re-name it?


Solution

  • The short answer to your question is that this particular key binding does different things depending on whether or not there is already text selected when you press the key, as outlined in Double Assignment of cmd + d on Mac OS in Sublime Text.

    If the cursor is within or at the start of a word, pressing the key will select that text, and then future selections will only match that exact word and not parts thereof.

    When text is selected first, the command will select the next instance of that text even if it's part of another word.

    The command itself is actually a derivative of the find command (the command in question is actually find_under_expand). As such, if you press the key while the cursor is on the symbol you want to refactor, it will select, and all other whole word instances of that text will also highlight as well (assuming that you have that option turned on in the Find panel).

    So, the general workflow might be to press ctrl+d to select the word and see what gets highlighted, both to see where other usages might me as well as to get the same context as using find would do.