Sublime Forum

Can i get `Create snippet` to put the cursor elsewhere on open?

#1

When i go to Create Snippet, by default it will always put the cursor at the end of the file. Is it possible to extend this functionality to get it to put the cursor in the snippet content part, where it says Hello, ${1:this} is a ${2:snippet}. by default?

0 Likes

#2

The last point of the tab field navigation can be controlled as the field with the index 0, however I’m not sure I understand your question.

0 Likes

#3

My question is about the process of creation of the snippet (not using it). When i first click “Create Snippet”, and get into the snippet creation file, that part of the file that says Hello, ${1:this} is a ${2:snippet}., is where my snippet content goes. I am wondering if my cursor can go there by default when that opens.

0 Likes

#4

You can do that, but to do so requires either creating a new plugin for generating the snippet stub or modifying the existing one.

An example of the latter option:

  1. Install PackageResourceViewer if you haven’t already done so
  2. Select PackageResourceViewer: Open Resource from the command palette (make sure you don’t accidentally pick one of the Extract options
  3. Select Default, then new_templates.py to open the plugin that’s responsible for creating new Sublime resource files by template
  4. Scroll down until you find the NewSnippetCommand command and replace that command with the one provided below
  5. Save the file
  6. Check the Sublime console with Ctrl+` or View > Show Console and make sure that it says reloading plugin Default.new_templates
class NewSnippetCommand(sublime_plugin.WindowCommand):
    def run(self):
        v = self.window.new_file()
        v.settings().set('default_dir', os.path.join(sublime.packages_path(), 'User'))
        v.settings().set('default_extension', 'sublime-snippet')
        v.assign_syntax('Packages/XML/XML.sublime-syntax')

        template = reformat(
            """
            <snippet>
            \t<content><![CDATA[
            ${0:Hello, \\${1:this\\} is a \\${2:snippet\\}.}
            ]]></content>
            \t<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
            \t<!-- <tabTrigger>hello</tabTrigger> -->
            \t<!-- Optional: Set a scope to limit where the snippet will trigger -->
            \t<!-- <scope>source.python</scope> -->
            </snippet>
            """)
        v.run_command("insert_snippet", {"contents": template})

Once the plugin reloads (which happens when you save it) the new command will be in place and ready to go. Creating a snippet will automatically select the content of the snippet for you to be able to edit it immediately.

You can of course adjust the template snippet as required; just remember to follow the format that the original is in. The most important part of that is noting that any $ characters in the snippet template that should appear as $ in the actual snippet when it’s created need to be specified as \\$ instead (as is done for $1 and $2 here) so that Sublime knows to leave them alone.

This procedure will create a folder named Default in your Packages folder (use Preferences > Browse Packages if you don’t know where that is) and put the altered version of new_templates.py inside of it. As long as that file exists at that location, Sublime will use it in place of the default; this is known as an override. Removing it will put things back the way they were should you want to do that.

The override will remain in place and active until you manually remove it, including if a new version of Sublime is released with a modified version of the file (which might contain more or better templates). For safety you may want to install the OverrideAudit package, which will let you know if that file gets updated in a future version of Sublime so that you won’t miss out on potential bug fixes or new features.

0 Likes