Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime text 2, ctrl+d not working as expected

It seems that 'quick add next' and 'expand selection to word' are both mapped to the same key binding.

I am not sure how to override/change the 'expand selection to word' because I cannot find the current key binding for both actions.

Does anyone know what the two actions are called?

Just to clarify, let's say I have the following block of code

this.GRID_TOP = 10;
this.GRID_SPACING = 10;
this.GRID_HEIGHT = 10;
this.GRID_WIDTH = 10;

I want to select the 'GRID' string on each line. I could use alt+F3 but that's overkill. I want to select 'GRID' then hit ctrl+D to select the subsequent matches. My problem is that it works once (selects first two) but then expands both cursors to the end of their words. Since the selections are now different, I cannot 'quick add next'.

like image 919
spinners Avatar asked Jun 04 '13 08:06

spinners


People also ask

What does Ctrl d do in Sublime Text?

Ctrl + D in SublimeText is "Quick Add Next." This appears to be equivalent to Ctrl + B in Brackets, which is "Add next match to Selection" on the Find menu.

How do I copy a line down in Sublime Text?

To quickly duplicate a line of code, place the cursor anywhere in the line and hit Cmd–Shift–D(Mac) or Ctrl–Shift–D (Windows).

How do you select all words in sublime?

Alt+F3 gives a really simple way to do find and replace: Use it to select all occurrences of the current word or selection, then just start typing to replace or edit them all at once.

What does Ctrl B do in Sublime Text?

In Sublime Text 3,when we press Ctrl+Shift+B, we are given the option to either do "Build and Run" or "only Build", whereas Ctrl+B executes the previously chosen operation among the two.


2 Answers

Bug conditions and effect

This seems to be a bug, or at least an unintuitive behaviour that only happens when the search bar is opened (after pressing ctrl+F, etc.) AND you selected a word part that is not already the string in the Find field (e.g. you searched for TOP before, and then you selected GRID).

The bug seems to be due to the specific behavior of find_under_expand while searching. In search mode, it will fill the Find field with the whole word under the caret on the first call, then starts finding and adding more occurrences to the selection. But it has some inconsistencies. For instance, if you click on different words one after the other, pressing ctrl+D after each click, and it will always fill the Find field with the word, but will alternatively not highlight the word and highlight it.

When you select a sub-word, it gets worse and only the next sub-word gets highlighted the first time you press ctrl+D. The following times, you get a mix of fill Find field, cursor goes to end of word and extend selection (a behavior that made sense on a single and complete word) which results in the selection of the first two words, with the second word (GRID_SPACING) filling the Find field.

Workaround

Solution 1

Close the search bar with esc and then use ctrl+D / Find > Quick Add Next repeatedly. Note that the selection won't be highlighted as searched strings (yellow in the default theme), but only as normal selection highlight (gray in the default theme).

Solution 2

If, after your first ctrl+D, you realized that your search bar was opened (and the Find field did not previously contain your target sub-word), it is not too late. Press ctrl+U / Edit > Undo Selection > Soft Undo to go back to the original sub-word selected. From here, use ctrl+D repeatedly. With the Find field containing the target sub-word, selection will behave as expected.

like image 164
hsandt Avatar answered Oct 31 '22 16:10

hsandt


Go to Preferences > Key Bindings - User

put following lines and save the file:

{ "keys": ["ctrl+d"], "command": "find_under_expand" },
{ "keys": ["ctrl+k"], "command": "find_under_expand_skip" },
{ "keys": ["ctrl+alt+d"], "command": "dpaste"}

It works for me. I've got installed DPaste package. That is why I had to overwrite its shortcut with the last line. Dpaste comes by default with ctrl+d

Hope it help

like image 24
roman Avatar answered Oct 31 '22 17:10

roman