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!
How to open a new c++ file with a predefined template in Sublime Text
sublimetexttext-editor
Related Question
- How to open files from Finder into existing instance of Sublime Text 2
- MacOS – Can’t open a file in sublime text using bash_rc
- MacOS – How does Sublime open file with cmd+click on its path in Terminal
- Set bash_profile to open with Sublime Text 2 by default
- MacOS – How to add link to Sublime Text
- Open file in Sublime Text tab via AppleScript
- Keyboard shortcut to open folder with Sublime Text
- How to make sublime text the default file viewer for all file types it supports
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):
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:
$
, you have to escape it like this:\$
.translateTabsToSpaces
istrue
.content
must be included in a<![CDATA[…]]>
section. Snippets won’t work if you don’t do this!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.