chenglou
chenglou

Reputation: 3640

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.

Upvotes: 5

Views: 2022

Answers (3)

yuliskov
yuliskov

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

Gerard Roche
Gerard Roche

Reputation: 6391

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.

Upvotes: 4

skuroda
skuroda

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

Related Questions