Sublime Forum

Define custom autocompletion for `New class`

#1

Hi there

I use ananconda for auto-completion (open to any other solution) and, currently, when I start coding a class the completion/pattern suggestion is

class ClassName(object):
    """docstring for ClassName"""
    def __init__(self, arg):
        super(ClassName, self).__init__()
        self.arg = arg

I would like to know what do I have to set up for this to be:

class ClassName:
    def __init__(self, arg):
        """docstring for init"""
        self.arg = arg

Thanks in advance!
Javier

0 Likes

#2

I guess there are several ways you can do that.

  1. Have a custom class snippet.
<snippet>
    <content><![CDATA[
class ${1:ClassName}:
    def __init__(self, ${2:arg}):
        """docstring for init"""
        self.${2:arg} = ${2:arg}
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>custom_class</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>source.python</scope>
</snippet>
  1. You could also override the default Python/Snippets/New-Class.sublime-snippet (the one that provides the first class boilerplate you have described) and replace the snippet contents of that with the one described in (1).
1 Like

#3

Hey @UltraInstinct05,

thank you!

quick question, where is Python/Snippets/New-Class.sublime-snippet

is it Sublime Text 3 -> Packages -> Python

because there I only have a file: Completion Rules.tmPreferences

Do I have to create a dir Snippets as: Sublime Text 3 -> Packages -> Python -> Snippets

Cheers!

0 Likes

#4

So what you have to do is to go to the Preferences -> Browse Packages ... from the main menu. This will bring you to the Packages directory. Here follow the given steps.

  1. Create the following folder structure Python/Snippets
  2. Inside the Snippets directory, create a new file called New-Class.sublime-snippet (The name matters !)
  3. Copy the following into it and save.
<snippet>
    <content><![CDATA[
class ${1:ClassName}:
    def __init__(self, ${2:arg}):
        """docstring for init"""
        self.${2:arg} = ${2:arg}
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>class</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>source.python</scope>
</snippet>

Now whenever you type class (the tab trigger) in a python file, you should get your choice of the class structure instead of the built in one.

1 Like

#5

Worked perfectly

Thanks!

0 Likes