Default code in a new file


#1

Is there a package or a way to put a default code in a new file on atom?


#2

Yes, but you’ll need to describe what you want in more detail. I don’t know of a specific package, but it’s a pretty easy init.coffee hack.


#3

Would be awesome if you could show how to add # -*- coding: utf-8 -*- to the beginning of every new file.


#4

That’s awfully broad. Are you sure you’re literally never going to be making a file that isn’t Python? You could write a snippet with the prefix #c that expands into that line.


#5

Thanks. I build the snippet like the manual said. How can I set it to be excecuted every time I create a new python file?


#6

You don’t. That’s not what snippets are for. You can add code to your init.coffee to automatically add text to a new file, but first I need to be absolutely sure I know what you want to do, hence my question earlier. Do you want any blank Python file to get encoding information added to it?


#7

Yes, exactly. Any blank Python file should contain this header. Thanks!


#8

Add this to your init.coffee:

atom.workspace.observeTextEditors (editor) ->
  python = atom.grammars.grammarForScopeName "source.python"
  if editor.isEmpty() and editor.getGrammar() == python
    editor.insertText "# -*- coding: utf-8 -*-"