Are you using ST3? If so you can leverage those command listeners If not, itâs a bit more involved (but still doable) as you would have to rebind the movement commands. If you have auto pairing enabled and you insert a closing quote, it does issue a move command, so the cursor will move, this may or may not be what you want. Then again, why would you enter a closing quote/brace/etc if you donât know where the cursor is?
[code]import sublime_plugin
import sublime
class MoveInterceptCommand(sublime_plugin.TextCommand):
def run(self, edit, by, forward, extend=False):
move_cursor_to_visible(self.view)
self.view.run_command(âmoveâ, {âbyâ: by, âforwardâ: forward, âextendâ: extend})
class MoveToInterceptCommand(sublime_plugin.TextCommand):
def run(self, edit, to, extend=False):
move_cursor_to_visible(self.view)
self.view.run_command(âmove_toâ, {âtoâ: to, âextendâ: extend})
class MoveInterceptEventListener(sublime_plugin.EventListener):
def on_text_command(self, view, command_name, args):
if len(view.sel()) == 1:
move_by_list = âcharactersâ, âlinesâ, âwordsâ, âword_endsâ, âsubwordsâ, âsubword_endsâ, âpagesâ]
move_to_list = âbolâ, âeolâ]
if command_name == âmoveâ:
if âbyâ in args and args"by"] in move_by_list:
return (âmove_interceptâ, args)
elif command_name == âmove_toâ:
if âtoâ in args and args"to"] in move_to_list:
return (âmove_to_interceptâ, args)
def move_cursor_to_visible(view):
vis_region = view.visible_region()
cursors = view.sel()
move_cursor_top = False
move_cursor_bottom = False
for cursor in cursors:
if cursor.b < vis_region.begin():
move_cursor_top = True
break
if cursor.b > vis_region.end():
move_cursor_bottom = True
break
if move_cursor_top:
cursors.clear()
cursors.add(sublime.Region(vis_region.begin(), vis_region.begin()))
elif move_cursor_bottom:
cursors.clear()
cursors.add(sublime.Region(vis_region.end(), vis_region.end()))
[/code]