Sublime Forum

Modify default key binding (Select all [Ctrl+A])

#1

So, I want modify default behavior, when Ctrl+A is pressed.
When there is single word selected, Ctrl+A must select all the same words. I made context-driven key binding in my Packages\User\Default.sublime-keymap:

{ "keys": ["ctrl+a"], "command": "find_all_under" , "context":
	[
		{ "key": "num_selections", "operator": "equal", "operand": 1 },
	]
}

It’s works fine: when the single word is selected: first keypress [ctrl+a] make all the same words selected. After that, second keypress [ctrl+a] make whole text selected, because num_selections isn’t equals 1 anymore, and (as I think), there is default key binding works.
BUT, when nothing is selected default [ctrl+a] not working, and I don’t understand why…

0 Likes

#2

Every caret that’s visible in a file represents a selection, and selections are allowed to be empty (nothing visible selected). You can verify this by opening the console with View > Show Console and entering len(view.sel()) when there’s nothing visibly selected:

>>> len(view.sel())
1

So whether there is anything visibly selected or not there’s (almost) always at least one selection, which makes your key binding always active; find_all_under does nothing if there’s no word under the cursor or selects the word under the cursor (and all others) if there is a word there, which makes your key binding essentially behave the same as the default key bound to this command.

It may work better for you if you use the selection_empty context instead of num_selections; that will only trigger when the selection is visibly present, which seems more like what you want:

{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
1 Like