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

10

458

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?

Factor answered 30/5, 2013 at 19:46 Comment(0)
E
655
  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

Elvinelvina answered 24/8, 2013 at 20:13 Comment(16)
For case-sensitive searching: #14829836Elvinelvina
how do you do this with ubuntu?Scandic
What is ⌘, how do you do this on a non-MAC keyboard?Rhizopod
at least in ST3 you can do Ctrl-⌘-G without the other ⌘-D command. Its terribly mislabeled in the menu unfortunately!Goutweed
@BjarkeFreund-Hansen see elaRosca's answer https://mcmap.net/q/80236/-how-to-select-all-instances-of-a-variable-and-edit-variable-name-in-sublimeElvinelvina
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.Extradite
@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 (https://mcmap.net/q/80236/-how-to-select-all-instances-of-a-variable-and-edit-variable-name-in-sublime).Elvinelvina
It's startlingly simple but not intuitive at all since I've tried everything except this. This helps soooo much, thanks!Hirudin
Does it find all instance of that variable or anything that matches that word. Because later is way less useful in some cases.Ignatzia
@MuhammadUmer Anything that matches the word, taking boundaries into account. Not a true variable search, but much better than simple string matching.Elvinelvina
i found sublime plugin, youtube.com/watch?v=P9K7mxWItPw, it allows you to rename instances of var/functions.Not just every wordIgnatzia
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?Benenson
@Nolan, your mention to Betjamin ends one of my growing frustration.Polymath
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" },Oscilloscope
@Benenson In Sublime Text 3, don't Ctrl D, simply Alt F3.Idolist
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.Marc
C
140

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.

Chronology answered 11/11, 2013 at 18:36 Comment(5)
its still selecting everything which has foo inside.Nehemiah
This answer is for the question: https://mcmap.net/q/81442/-how-to-select-all-instances-of-selected-region-in-sublime-text/2270563. But here the question is different. To select only the variables rather than all matches.Seedbed
@Nehemiah 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. VoilaPolymath
This answer is wrong. This will select all instance of foo not just the highlighted ones.Lanlana
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.Piperpiperaceous
E
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)

Eaglet answered 30/5, 2013 at 20:1 Comment(3)
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 foos, 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!Elvinelvina
@Nolan - feel free to ask a question 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.Eaglet
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 #12162547 - that one says "highlighted selection". Also, of course, foo often shows up as (foo), _foo,, (foo,, and _foo}, so _foo_ is pretty unsatisfactory.Elvinelvina
U
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.

Unasked answered 23/9, 2013 at 21:13 Comment(5)
This was the answer that worked for me apart for the answer that have been mentioned. Good !Tourane
This does not answer the question.Holsworth
@Holsworth How so ?Unasked
@Unasked 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.Holsworth
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.Unasked
N
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 .

Nehemiah answered 2/6, 2015 at 1:45 Comment(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.Apricot
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.Dagny
T
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.

Tufts answered 13/3, 2017 at 16:5 Comment(3)
This is the only answer that actually answers the original question!Doggone
Still selects strings and commentsPome
Ctrl-Shit-D is a rather standard shortcut for 'duplicate selection' thoughMarianelamariani
A
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.

Apricot answered 6/8, 2016 at 22:4 Comment(0)
I
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.

Idolist answered 12/5, 2017 at 7:52 Comment(0)
L
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.

Lusaka answered 17/10, 2020 at 23:8 Comment(0)
V
-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

Valorize answered 30/8, 2018 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.