Skip to content

Instantly share code, notes, and snippets.

@jaredyam
Last active February 23, 2021 02:07
Show Gist options
  • Save jaredyam/e73408e04f5e9e5f14bab26dd0b5a205 to your computer and use it in GitHub Desktop.
Save jaredyam/e73408e04f5e9e5f14bab26dd0b5a205 to your computer and use it in GitHub Desktop.

Improved Keymap of Sublime Text 3 AutoDocstring Package

TL;DR

Auto-add docstrings by entering the newline when it's needed.

Prerequisite packages

  1. Package Control
  2. AutoDocstring

Steps to add the custom keymap

  1. Select Sublime Text -> Preferences -> Key Bindings

  2. Add the following json chunk:

     {
        "keys": ["enter"],
        "command": "auto_docstring",
        "context": [
        {
            "key": "preceding_text",
            "operator": "regex_match",
            "operand": "^ *(def .*\\).*:|class .*:|[^\\(\\n]*\\).*:)",
            "match_all": true
        },
        {
            "key": "setting.auto_indent",
            "operator": "equal",
            "operand": true,
            "match_all": true
        },
        {
            "key": "selection_empty",
            "operator": "equal",
            "operand": true,
            "match_all": true
        },
        {
            "key": "auto_complete_visible",
            "operator": "equal",
            "operand": false,
            "match_all": true
        },
        {
            "key": "selector",
            "operator": "equal",
            "operand": "source.python"
        }, ]
    },

Side notes

  1. The regexp pattern is inspired from the comment from @Damodar Bashyal in the [Stack Overflow] Regular expression to match a line that doesn't contain a word.
  2. If you don't want to add docstrings in some cases, you can simply insert a newline with the built-in command ⌘ + enter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment