How to open a new c++ file with a predefined template in Sublime Text

sublimetexttext-editor

I am keen to know if there is a way to open not an empty new file but a new file with some predefined template. I participate in online coding contests and for every new file (which I save as a '.cpp'), I have to Cmd+C and Cmd+V all the preprocessor directives i.e. #include <..>'s and #define's from one of my earlier '.cpp' file. Is there a way by which I can have all these things written to some file and every new file that I save as a c++ file this data gets loaded onto it thus saving my copy-pasting/retyping. Eclipse IDE has this feature, I wanted to know if its possible somehow in Sublime Text too. Thanx!

Best Answer

You should use a snippet. For instance, you can define a new snippet and trigger it with !initcpp. Then you open a new file, type !initcpp, hit and your are ready to go.

To define a new one go to Tools → New Snippet…

From Sublime Text: Snippets.

Snippets can be stored under any package’s folder, but to keep it simple while you’re learning, you can save them to your Packages/User folder.

Snippets File Format

Snippets typically live in a Sublime Text package. They are simplified XML files with the extension .sublime-snippet.

The structure of a typical snippet is as follows (including the default hints Sublime Text inserts for your convenience):

<snippet>
    <content><![CDATA[Type your snippet here]]></content>
    <!-- Optional: Tab trigger to activate the snippet -->
    <tabTrigger>xyzzy</tabTrigger>
    <!-- Optional: Scope the tab trigger will be active in -->
    <scope>source.python</scope>
    <!-- Optional: Description to show in the menu -->
    <description>My Fancy Snippet</description>
</snippet>

The snippet element contains all the information Sublime Text needs in order to know what to insert, whether to insert and when. Let’s look at each of these parts in turn.

content

The actual snippet. Snippets can range from simple to fairly complex templates. We’ll look at examples of both later.

Keep the following in mind when writing your own snippets:

  • If you want to get a literal $, you have to escape it like this: \$.
  • When writing a snippet that contains indentation, always use tabs. When the snippet is inserted, the tabs will be transformed into spaces if the option translateTabsToSpaces is true.
  • The content must be included in a <![CDATA[…]]> section. Snippets won’t work if you don’t do this!
  • The content of your snippet must not contain ]]> because this string of characters will prematurely close the <![CDATA[…]]> section, resulting in an XML error. To work around this pitfall, you can insert an undefined variable into the string like this: ]]$NOT_DEFINED>. This modified string passes through the XML parser without closing the content element’s <![CDATA[…]]> section, but Sublime Text will replace $NOT_DEFINED with an empty string before inserting the snippet into your file. In other words, ]]$NOT_DEFINED> in your snippet file content will be written as ]]> when you trigger the snippet.

tabTrigger

Defines the sequence of keys that must be pressed to insert this snippet. After typing this sequence, the snippet will kick in as soon as you hit the key.

A tab trigger is an implicit key binding.

scope

Scope selector determining the context where the snippet will be active. See Scopes for more information.

description

Used when showing the snippet in the Snippets menu. If not present, Sublime Text defaults to the file name of the snippet.

With this information, you can start writing your own snippets as described in the next sections.