Last Updated: February 25, 2016
·
1.575K
· darkliquid

Disabling sort lines in Sublime Text 2 so it only works on selections

I've occasionally pressed F9 whilst in ST2 and had it 'helpfully' sort all my code in the current file, which naturally results in a terribly broken file 99% of the time. However, the sort line functionality is useful, but generally I never want to fun it on an entire file, just a selection so for safeties sake, you can modify the keybindings to do just that!

In your user keybindings file, add the folling keybinding directives to the keybindings array:

{ "keys": ["f9"], "command": "unbound" },
{ "keys": ["ctrl+f9"], "command": "unbound" },
{ "keys": ["f9"], "command": "sort_lines", "args": {"case_sensitive": false}, "context": [
  { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
] },
{ "keys": ["ctrl+f9"], "command": "sort_lines", "args": {"case_sensitive": true}, "context": [
  { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
] }

This will unbind the commands, then rebind them, but within an additional context of only applying when selection_empty is false i.e. there is a selection.