Sublime Forum

Selecting the next instance of a variable, skipping other text that matches variable name

#1

Hi, first time poster, been using ST for years but I’m just getting into the meat of all the keyboard shortcut stuff. Gotta look in the Selection menu more…

So, I’m cleaning up some old python code, and I come across some one-letter variables:

for t in tilelist:
    if (type(t) != str):
        t = t.id

The instances of t have an outline, but if I press Cmd+D, it selects the ts in type and str.

Is there a keyboard shortcut that works like Cmd+D, but would only select those outlined instances of t?

0 Likes

#2

Cmd+D (or on other platforms, Ctrl+D) is the way to do this, so you’re on the right track.

If you select the t first and then press the key binding, you’re telling Sublime that you want to select all instances of the selected text, so it will pick up the t characters in tilelist, type and str.

If you instead put the first on the t and use the first key press to tell it select that word, it will automatically select only whole words (i.e. the ones you want):

3 Likes

#3

Perfect! Thanks so much!

0 Likes

#4

As a side note, when you’re performing this operation, Cmd+D will accept the currently selected text and move on to the next item. You can also use Cmd+KCmd+D (i.e. press one key then the other in quick sucession) to tell Sublime to reject the currently selected item and move ahead to the next one.

2 Likes

#6

how do i undo this delete of a post? i dont wan t to delete my post i wanted to EDIT it

0 Likes

#7

I have had this problem numerous times where for example I might want to change all instances of a variable called “this” to a variable called “that” and where i have a comment that reads

// this is a comment

And I dont want to modify the comment at all. Marking the first instance of “this” and hitting control d repeatedly is still going to mark instances of items I do not want to change. There should be a way to UNDO most recently selected item and move to next item. maybe alt d would work.

0 Likes

#8

You can use the sequence I mentioned above when you get to the that in a comment to skip over it and go on to the next item. You can also use Ctrl+U to undo the last selection, if you accidentally go too far. I’m not sure offhand what the MacOS key binding for that is though.

0 Likes

#9

I can confirm it’s just Cmd+U on MacOS.

If you select the one in the comment though, you can press Cmd+K then the next Cmd+D will unselect it and select the next one instead.

1 Like

#10

THATS how you do it!
lol
ty :slight_smile:

0 Likes