New file initialization


#1

Good day everyone, i enjoy using Atom text editor and want to edit my initialization script.

I’m not good in Python(scripts are written in Python) and want to know, what i should write in my Init Script for opening all new files with extension .c in such format:

# include < stdio.h>

int main(void){

getchar(); getchar();
return 0;
}

Thanks for help and good luck.


#2

Atom’s scripts are written in JavaScript or CoffeeScript. init.coffee is CoffeeScript. The following code should do what you want:

atom.workspace.observeTextEditors (editor) ->
  template = '''
# include < stdio.h>

int main(void){

  getchar(); getchar();
  return 0;
}
'''

  if editor.getGrammar() is 'source.c' and editor.isEmpty()
    editor.setText(template)

#3

i tried your code and nothing really happened, i opened new file and it’s still empty. Even when i try to open it via Folder --> New File. Although, thank you for help and reply )


#4

Okay, I figured out what was wrong. atom.workspace.getActiveTextEditor().getGrammar() returns a big object, not a string. I had to grab the scopeName key from it.

atom.workspace.observeTextEditors (editor) ->
  template = '''
  # include < stdio.h>

  int main(void){

    getchar(); getchar();
    return 0;
  }
  '''

  if editor.getGrammar().scopeName is 'source.c' and editor.isEmpty()
    editor.setText(template)

New file initialization(re)
#5

Appreciate you a lot, this solution really helped me and increased my performance )