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.
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)
In vintage Mode
;
and put back in command mode Esc You can replay your macro: @ followed by YOUR_MACRO_ID
Packages/User/Macros/complete-semi-colon.sublime-macro
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With