454

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?

Nolan Amy
  • 10,785
  • 5
  • 34
  • 45
Oskar Persson
  • 6,605
  • 15
  • 63
  • 124

10 Answers10

650
  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

Community
  • 1
  • 1
Nolan Amy
  • 10,785
  • 5
  • 34
  • 45
  • For case-sensitive searching: http://stackoverflow.com/questions/14829336/case-sensitive-cmdd-in-sublime-text-2 – Nolan Amy Nov 11 '13 at 22:18
  • how do you do this with ubuntu? – Connor Leech Mar 19 '14 at 09:22
  • 14
    What is ⌘, how do you do this on a non-MAC keyboard? – Bjarke Freund-Hansen May 06 '14 at 09:38
  • 8
    at least in ST3 you can do Ctrl-⌘-G without the other ⌘-D command. Its terribly mislabeled in the menu unfortunately! – watsonic Jun 04 '14 at 21:00
  • @BjarkeFreund-Hansen see elaRosca's answer http://stackoverflow.com/a/19913390/483520 – Nolan Amy Jun 06 '14 at 23:54
  • 34
    This doesn't answer the OP's original question. He specifically asked `variable (not just any string)`. This simply selects all occurrences of the string. – Betjamin Richards Nov 22 '14 at 09:53
  • 19
    @BetjaminRichards Make sure you're not selecting (highlighting) the variable. Just move the cursor onto the variable and then press ⌘D. Incidentally, this is exactly the issue I had with MattDMo's answer (http://stackoverflow.com/a/16844924/483520). – Nolan Amy Nov 22 '14 at 17:59
  • 1
    It's startlingly simple but not intuitive at all since I've tried everything except this. This helps soooo much, thanks! – Vexter Mar 03 '15 at 09:17
  • Does it find all instance of that variable or anything that matches that word. Because later is way less useful in some cases. – Muhammad Umer May 08 '15 at 16:48
  • 2
    @MuhammadUmer Anything that matches the word, taking boundaries into account. Not a true variable search, but much better than simple string matching. – Nolan Amy May 08 '15 at 21:49
  • i found sublime plugin, https://www.youtube.com/watch?v=P9K7mxWItPw, it allows you to rename instances of var/functions.Not just every word – Muhammad Umer May 09 '15 at 01:26
  • I am using ST3 and Windows. Your second example is what I was searching for, but the equivalent command,'Alt+F3', giving to replace yours 'Ctrl⌘G', is not running like the example, because it select all string ocurrences, not just instances, like it should do. Any idea of what is happen? – Doglas May 10 '16 at 16:09
  • @Nolan, your mention to Betjamin ends one of my growing frustration. – Anwar Jun 07 '16 at 09:34
  • 1
    You can set up your own hotkey for it. The command is `find_all_under`. E.g.: Go to Preferences -> Key bindings, insert `{ "keys": ["alt+d"], "command": "find_all_under" },` – thorbjornwolf Oct 10 '16 at 15:35
  • 2
    @Doglas In Sublime Text 3, don't `Ctrl` `D`, simply `Alt` `F3`. – Franklin Yu May 12 '17 at 07:53
  • VScode has `F2` hotkey that renames all instances in all files that import the variable / class. Extremely useful. Same is possible with `Ctrl + T, Ctrl + M` after installing typescript package. At the bottom of the screen an input where the name can be edited is presented. This is a far more superior solution than `Alt + F3`, which is constrained to the local file. – Adrian Moisa Dec 20 '17 at 19:38
139

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.

elaRosca
  • 3,083
  • 4
  • 21
  • 22
  • 10
    its still selecting everything which has foo inside. – user1767754 Jun 02 '15 at 01:35
  • 1
    This answer is for the question: http://stackoverflow.com/q/12162047/2270563. But here the question is different. To select only the variables rather than all matches. – RamValli Jan 20 '16 at 09:40
  • 4
    @user1767754 If you don't want to select all that has `foo` inside, don't already select the first `foo`. Instead just place a cursor in the word `foo` and then `Alt-F3`. Voila – Anwar Jun 07 '16 at 09:35
  • This answer is wrong. This will select all instance of `foo` not just the highlighted ones. – André C. Andersen Sep 26 '16 at 08:50
  • Doesn't work in KDE, so I just added { "keys": ["alt+d"], "command": "find_all_under" } to the key bindings file: Preferences > Key BIndings. alt+d doesn't seem to conflict with anything there. – user1985553 May 03 '17 at 17:17
53

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)

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • 6
    But the stroke appears only around occurrences of the *variable name*, while `⌘+D` acts on all occurrences of the string. For example, if I highlight `foo`, sublime wonderfully strokes all the variable `foo`s, but not the string `foo` in my `eject_buffoon` function. And yet using the key combos above, I'm forced to manually `⌘+K` out the string matches to avoid having an `eject_bufbarn` function. There must be a better way! – Nolan Amy Aug 18 '13 at 19:05
  • @Nolan - feel free to [ask a question](http://stackoverflow.com/questions/ask) and see if anyone has any suggestions. One possible workaround is to select `foo_` (foo and the space after it) or `_foo_` (spaces both before and after) and then use `⌘-D` or `Ctrl-⌘-D`. – MattDMo Aug 18 '13 at 23:32
  • 7
    I actually think this is exactly the question that the OP was asking. He specifically says "*variable*" (not "text region" or "string") and "*stroke*" (not "highlighting"). And this question is *not* a duplicate of http://stackoverflow.com/questions/12162047/sublime-text-editor-how-to-select-all-instances-of-selected-region - that one says "highlighted selection". Also, of course, `foo` often shows up as `(foo)`, `_foo,`, `(foo,`, and `_foo}`, so `_foo_` is pretty unsatisfactory. – Nolan Amy Aug 19 '13 at 06:16
21

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.

Community
  • 1
  • 1
hobs
  • 18,473
  • 10
  • 83
  • 106
  • 1
    This was the answer that worked for me apart for the answer that have been mentioned. Good ! – Akash Jun 10 '17 at 14:39
  • This does not answer the question. – MD004 Jul 30 '19 at 19:26
  • @MD004 How so ? – hobs Jul 31 '19 at 18:35
  • @hobs The question is how to "select all of those instances" *at once*. I'll admit the phrasing of the question in the title is ambiguous, but later in the post the OP describes your solution and says that this "selects them one-by-one, but I have to manually exclude the non-variable string matches" which is unsatisfactory. Btw, an example scenario where this is unsatisfactory is when there are, say, 1000 occurrences of the word you want to replace, in which case manually selecting them is not an option. – MD004 Aug 01 '19 at 18:03
  • 1
    Ahh, I see. Thank you. Now I see why the accepted answer is much better for the OP's situation and your 1000-occurence example. – hobs Aug 04 '19 at 02:29
13

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 .

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user1767754
  • 23,311
  • 18
  • 141
  • 164
  • 2
    This is the answer for variables of single characters. For any other variables, you can put the cursor anywhere in the variable to start with. – Manish Singh Aug 06 '16 at 21:53
  • This works for me in Sublime 3 and is the simplest way to do it if you want to limit the changes to a smaller area like within a function as opposed to the whole file. Command-D for macOS, of course. – Ryan H. Mar 17 '18 at 17:22
10

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
6

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Manish Singh
  • 5,848
  • 4
  • 43
  • 31
5

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.

Franklin Yu
  • 8,920
  • 6
  • 43
  • 57
0

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.

-1

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

Jay
  • 594
  • 10
  • 23