Sublime Text: escape multiple selections to last selection region instead of first


In Sublime Text (using Sublime Text 3), you can get out of a multiple selection / multiple caret mode by pressing the Esc key. When you do this, only the first selection / caret will remain.

Is there a way to make it so that only the last selection / caret will remain instead?

I often find myself hitting escape then needing to move my cursor to what used to be the last selection.

Use case:

I type this using multiple selection:

int foo(
  int x,
  int x,
  int x,
  int x,

and I want to get rid of the last comma to get to:

int foo(
  int x,
  int x,
  int x,
  int x

I'd like to hit the Esc key and have my caret be right after that last int x,.

Best Answer

I think this is not a default command in sublime text. However you can easily create this behavior on your own. Just press Tools >> New Plugin... and paste the following:

import sublime_plugin

class SingleLastSelectionCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        if len(view.sel()):
            last = view.sel()[-1]

Afterwards create a keybinding:

{ "keys": ["escape"], "command": "single_last_selection", "context":
        { "key": "num_selections", "operator": "not_equal", "operand": 1 }

It might be necessary to copy all escape keybinding from the default keymap to keep the action precedence (hide autocompletion before removing multiple cursors and so on).

Related Question