Sublime Forum

How to edit autocomplete format for any syntax

#1

Hello,

When I use the autocomplete for syntax; for example, “For loop” in C language.
The syntax format will normally show as below:

for (int i = 0; i < count; ++i)
{
/* code */
}

I would like to know how can I change the position of the ‘{’ to the upper line to match my coding style as below:

for (int i = 0; i < count; ++i) {
/* code */
}

Of course it might not be a big issue but it might feel a bit annoying for some coder who using the same style of coding.
Thank you in advance.

0 Likes

#2

That is actually a snippet, which is provided by the C++ package that ships with Sublime (that package covers both C and C++ due to their similarities).

The snippet in question is stored in Packages\C++\Snippets\030-for-int-loop-(fori).sublime-snippet, and looks like this:

<snippet>
    <description>For Loop</description>
    <content><![CDATA[for (int ${2:i} = 0; $2 < ${1:count}; ${3:++$2})
{
    ${0:/* code */}
}]]></content>
    <tabTrigger>for</tabTrigger>
    <scope>source.c, source.objc, source.c++, source.objc++</scope>
</snippet>

See the link above for more information on the format of the snippet file itself. Something to note is that the XML CDATA portion of the snippet is important; if you remove it, the snippet won’t work.

This file is actually stored inside of a sublime-package file that ships with Sublime, so it’s not a good idea to modify it directly, as the change will be lost on Sublime update/reinstall.

You could duplicate the above snippet by selecting Tools > Developer > New Snippet... from the menu and replacing the contents of the generated snippet stub with the code above, shifting the location of the opening brace as appropriate.

An issue with this is that the existing snippet in the C++ package will still remain, which means that there will be two identical looking completions offered, only one of which being the one that you want.

You can stop this from happening by creating an override for the original file, which will mask its contents with what you provide without directly modifying the package. Such an override remains in your Packages directory between reinstalls and updates so it’s contents aren’t lost like they would be if you modified the package file directly.

The easiest way to create an override for this would be:

  1. Install PackageResourceViewer if you haven’t already (it’s an invaluable tool for looking inside of and tweaking packages)

  2. Open the command palette via Tools > Command Palette or the associated key for your platform (visible in the menu if you don’t know it offhand)

  3. Enter the text prv:o and select the PackageResourceViewer: Open Resource command from the list

  4. Select the C++ package, then Snippets, then 030-for-int-loop-(fori).sublime-snippet (you can use fuzzy searching here to navigate the list faster)

  5. Either erase the entire contents of the file OR modify it as appropriate for your uses (see below)

  6. Save the file; PackageResourceViewer will save it as an override file automatically in the correct location.

In step #5 above, you have a choice; you can edit the snippet directly to format the code the way you want it, or you can erase the entire contents and save it as an empty file. In the first case that’s the only step you need to do to make the snippet work like you want. In the second case that will remove the existing snippet, in which case you also need to provide your own as outlined above.

Note that an override remains forever, so your override will remain in place even if the official version of the file changes in a future Sublime release. I’m having a hard time envisioning improvements in the world of for loops, though, so that’s probably nothing to be concerned about. :wink:

2 Likes

#3

Thank you for your reply. It seems that I have to override every single syntax and of course on every language if I need to make it that way. May be, the easiest way to solve this issue is to change my coding style :smiley:

0 Likes

#4

unfortunately, that is how it is at the moment. but see also this possible solution:

0 Likes