Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text binding: insert semicolon at eol and come back

I'm new to Sublime Text key bindings. Is there a way, when the caret isn't at the end of the line, to insert a semicolon at the end? In macro I guess it'd be: go to eol -> insert ; -> come back. But I'm not sure how to do the come back part.

Thanks.

like image 591
chenglou Avatar asked Jun 06 '13 01:06

chenglou


2 Answers

You would have to use a plugin I think since you want to restore the previous position, though I could be wrong. This is an ST3 version.

import sublime
import sublime_plugin

class SemicolonInsertCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        region_name = "original_cursors"
        view = self.view
        view.add_regions(region_name, view.sel())
        view.run_command("move_to", {"extend": False, "to": "eol"})
        view.run_command("insert", {"characters": ";"})
        view.sel().clear()
        cursors = view.get_regions(region_name)
        for cursor in cursors:
            view.sel().add(sublime.Region(cursor.b, cursor.b))
        view.erase_regions(region_name)

Create a key binding with the command semicolon_insert. I assumed your macro definition was supposed to be eol not eof.

Edit: ST2 compatible version

import sublime
import sublime_plugin

class SemicolonInsertCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        region_name = "original_cursors"
        view = self.view
        view.add_regions(region_name, list(view.sel()), "")
        view.run_command("move_to", {"extend": False, "to": "eol"})
        view.run_command("insert", {"characters": ";"})
        view.sel().clear()
        cursors = view.get_regions(region_name)
        for cursor in cursors:
            view.sel().add(sublime.Region(cursor.b, cursor.b))
        view.erase_regions(region_name)
like image 131
skuroda Avatar answered Sep 28 '22 14:09

skuroda


Record a macro, save it, and configure a shortcut for it.

Create the Macro

In vintage Mode

  • Start recording macro q followed by MACRO_ID ( LETTER [a-z] )
  • Create a bookmark Ctrl+F2
  • Move to end of line and into insert mode Shift+A
  • Add the semi-colon ; and put back in command mode Esc
  • Go to bookmark Shift+F2
  • Remove bookmark Ctrl+Shift+F2
  • Stop recording macro q

You can replay your macro: @ followed by YOUR_MACRO_ID

Save the macro

  • Go to: Tools > Save Macro...
  • Name it and save it e.g. Packages/User/Macros/complete-semi-colon.sublime-macro

Create a shortcut for the macro

  • Open your Key Bindings config file: Preferences > Key Bindings - User
  • Create a shortcut for your new macro e.g. Ctrl+; e.g.

    { 
        "keys": ["ctrl+;"], 
        "command": "run_macro_file", 
        "args": {
            "file": "Packages/User/Macros/complete-semi-colon.sublime-macro"
        } 
    }
    

You can do something similar without vintage mode. The important parts are the bookmark, and the macro shortcut configuration.

Enjoy.

like image 32
Gerard Roche Avatar answered Sep 28 '22 14:09

Gerard Roche