Search code examples
sublimetext3sublimetextc89

Sublime Text C89 autocompletion


I'm using ST3 and code in the C89 standard. The super useful for-loop auto completion completes to:

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

But this does not honor the standard. Can this behavior be changed in some config file to complete to:

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

?


Solution

  • This is actually a snippet which is provided by c++ package that ships with sublime (that package covers both C and C++ due to their similarities).

    The snippet which is responsible for the auto complete is 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>
    

    You can modify that as you prefer. To do this,

    1. Install PackageResourceViewer for sublime. It lets you to look inside sublime packages.
    2. Open the Command pallete using ctrl+shift+p.
    3. Enter prv:o and select PackageResourceViewer: Open Resource from the list.
    4. Type C++ and navigate to Snippets\030-for-int-loop-(fori).sublime-snippet
    5. Modify the code to with this.

      <snippet>
           <description>For Loop</description>
           <content>
           <![CDATA[int ${2:i} = 0;
      for ( ${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>
      
    6. And save the file.