How to select all instances of a variable and edit variable name in Sublime

Keyboard ShortcutsSublimetextKey Bindings

Keyboard Shortcuts Problem Overview


If I select a variable (not just any string) in my code, all other instances of that variable get a stroke (white outline) around them:

enter image description here

Is there a keyboard shortcut that will let me select all of those instances of the variable and edit them all at once?


Things I've Tried:

D, K, and U lets me select them one-by-one, but I have to manually exclude the non-variable string matches:

enter image description here

And using CtrlG simply selects all the string matches:

enter image description here

Clearly, Sublime is able to differentiate between variable and string matches. Is there no way to select just the variable matches?

Keyboard Shortcuts Solutions


Solution 1 - Keyboard Shortcuts

  1. Put the cursor in the variable.

    Note: the key is to start with an empty selection. Don't highlight; just put your cursor there.

text cursor on variable

  1. Press D as needed. Not on a Mac? Use CtrlD.

more instances of variable highlighted

Didn't work? Try again, making sure to start with nothing selected.

More commands:

> Find All: CtrlG selects all occurences at once. Not on a Mac? AltF3 > > Undo Selection: U steps backwards. Not on a Mac? CtrlU > > Quick Skip Next: KD skips the next occurence. Not on a Mac? CtrlKCtrlD > > Sublime Docs

Solution 2 - Keyboard Shortcuts

I know the question is about Macs, but I got here searching the answer for Ubuntu, so I guess my answer could be useful to someone.

Easy way to do it: AltF3.

Solution 3 - Keyboard Shortcuts

Despite much effort, I have not found a built-in or plugin-assisted way to do what you're trying to do. I completely agree that it should be possible, as the program can distinguish foo from buffoon when you first highlight it, but no one seems to know a way of doing it.


However, here are some useful key combos for selecting words in Sublime Text 2:

CtrlG - selects all occurrences of the current word (AltF3 on Windows/Linux)

D - selects the next instance of the current word (CtrlD)

  • K,D - skips the current instance and goes on to select the next one (CtrlK,CtrlD)
  • U - "soft undo", moves back to the previous selection (CtrlU)

E, H - uses the current selection as the "Find" field in Find and Replace (CtrlE,CtrlH)

Solution 4 - Keyboard Shortcuts

This worked for me. Put your cursor at the beginning of the word you want to replace, then

CtrlK, CtrlD, CtrlD ...

That should select as many instances of the word as you like, then you can just type the replacement.

Solution 5 - Keyboard Shortcuts

The Magic is, you have to start with an empty selection, so put your cursor in front of the word/character you want to multi-select and press Ctrl+D .

Solution 6 - Keyboard Shortcuts

To me, this is the biggest mistake in Sublime. Alt+F3 is hard to reach/remember, and Ctrl+Shift+G makes no sense considering Ctrl+D is "add next instance to selection".

Add this to your User Key Bindings (Preferences > Key Bindings):

{ "keys": ["ctrl+shift+d"], "command": "find_all_under" },

Now you can highlight something, press Ctrl+Shift+D, and it will add every other instance in the file to the selection.

Solution 7 - Keyboard Shortcuts

As user1767754 said, the key here is to not make any selection initially.

Just place the cursor inside the variable name, don't double click to select it. For single character variables, place the cursor at the front or end of the variable to not make any selection initially.

Now keep hitting Cmd+D for next variable selection or Ctrl+Cmd+G for selecting all variables at once. It will magically select only the variables.

Solution 8 - Keyboard Shortcuts

It's mentioned by @watsonic that in Sublime Text 3 on macOS, starting with an empty selection, simply G (AltF3 on Windows) does the trick, instead of D + G in Sublime Text 2.

Solution 9 - Keyboard Shortcuts

At this moment, 2020-10-17, if you select a text element and hit CTRL+SHIFT+ALT+M it will highlight every instance within the code chunk.

Solution 10 - Keyboard Shortcuts

Just in case anyone else stumbled on this question while looking for a way to replace a string across multiple files, it is Command+Shift+F

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOskar PerssonView Question on Stackoverflow
Solution 1 - Keyboard ShortcutsNolan AmyView Answer on Stackoverflow
Solution 2 - Keyboard ShortcutselaRoscaView Answer on Stackoverflow
Solution 3 - Keyboard ShortcutsMattDMoView Answer on Stackoverflow
Solution 4 - Keyboard ShortcutshobsView Answer on Stackoverflow
Solution 5 - Keyboard Shortcutsuser1767754View Answer on Stackoverflow
Solution 6 - Keyboard ShortcutsTravis HeeterView Answer on Stackoverflow
Solution 7 - Keyboard Shortcutsmanish_sView Answer on Stackoverflow
Solution 8 - Keyboard ShortcutsFranklin YuView Answer on Stackoverflow
Solution 9 - Keyboard Shortcutsphillip perinView Answer on Stackoverflow
Solution 10 - Keyboard ShortcutsJayView Answer on Stackoverflow