Default file templates


#1

Everyone has it’s own way to start a file, it doesn’t matter if you program in C, Java, JavaScript, Ruby or whatever language you want to use, you almost always has a standard way to start your files with some comments (sometimes some documentation), some default code structure, etc.

There’s any package so far to do and control this in Atom?

Some package it lets me create a new .js file with my favorite code structure already in it just after been created?

How hard it could be to develop one?


Do atom support file-template?
#2

I was looking for this exact thing this morning, and realized that snippets take care of this rather well. It’s a little unwieldy on larger file templates because you need to make the file compatible with a CSON string (replace newlines with \n, etc). But after you’ve done that you have all the advantages of snippets, like placeholders and the ability to create multiple named templates/snippets.

I wrote up a little sed command to convert a template python file into a one line CSON string that was prepared for snippets. I’m certain it isn’t exhaustive, but it worked for my needs.

sed -e ':a;N;$!ba;s/\n/\\n/g' -e "s/'/\\\'/g" /path/to/my/template/file.py

The first sed command replaces newlines with a literal “\n”. The second one replaces single quotes with escaped single quotes.

If you’re doing this on mac, you’ll need gnu-sed, since OSX sed works differently than the gnu version:

brew install gnu-sed

Then just use “gsed” rather than sed.


#3

You know you can write snippets with multi-line bodies too. From the snippets package README:

'.source.js':
  'if, else if, else':
    'prefix': 'ieie'
    'body': """
      if (${1:true}) {
        $2
      } else if (${3:false}) {
        $4
      } else {
        $5
      }
    """

The triple-double-quote in CoffeeScript allows you to write block strings.


#4

Hah! Yeah when I broke out sed, I had a nagging feeling that I was doing it wrong.

This works great! Thanks!