Reputation: 3640
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.
Upvotes: 5
Views: 2022
Reputation: 1498
Slight modification to code above. Add toggle behavior. So when you again invoke key combination semicolon will be removed. This is Sublime Text 3 version.
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"})
pos = view.sel()[0].begin()
last_chr = view.substr(pos-1)
if last_chr != ';':
view.run_command("insert", {"characters": ";"})
else:
view.run_command("left_delete")
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)
To add keyboard shortcut insert next line to "Preferences > Key Bindings - User":
{ "keys": ["ctrl+alt+enter"], "command": "semicolon_insert" }
Upvotes: 2
Reputation: 6391
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.
Upvotes: 4
Reputation: 19744
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)
Upvotes: 6