Skip to content

Instantly share code, notes, and snippets.

@Azeirah
Created March 25, 2016 17:27
Show Gist options
  • Save Azeirah/3f3d43e32ab799c66cfc to your computer and use it in GitHub Desktop.
Save Azeirah/3f3d43e32ab799c66cfc to your computer and use it in GitHub Desktop.
Sublime Text - Automatic reindentation while swapping

Sublime Text has a somewhat hidden feature to automatically reindent the current line. It would be wasteful to bind this to a keybinding, I prefer it to be automatic.

print("Executing a function")
def someFunction():
    return 5

Moving the print line one line down with ctrl+shift+down will result in

def someFunction():
    print("Executing a function")
    return 5

Instead of

def someFunction():
print("Executing a function")
    return 5

Installation

I hacked this together, if someone could make a plugin for this, I would be very grateful :)

First, drop this file into your Sublime's Packages/User folder. Name it run_multiple_commands.py.

Now open your user keybindings, and insert this snippet:

    {
        "keys": ["ctrl+shift+up"],
        "command": "run_multiple",
        "args": {
            "commands": [
                {"command": "swap_line_up", "context": "window"},
                {"command": "reindent", "context": "window"}
            ]
        }
    }, {
        "keys": ["ctrl+shift+down"],
        "command": "run_multiple",
        "args": {
            "commands": [
                {"command": "swap_line_down", "context": "window"},
                {"command": "reindent", "context": "window"}
            ]
        }
    }

How does it work? The linked script, run_multiple_commands.py let's you run multiple commands (duh) on keybindings, by simply appending the reindent command to the swap_line_up and swap_line_down commands, you get automatic reindentation! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment