Set a default syntax by filetype?


For the time being, I’d like to tell my Handlebars files to use HTML syntax highlighting. I assume I can do that in but I’m unaware how to find the available methods to call on editor as per the example there.

    atom.workspaceView.eachEditorView (editorView) ->
      editor = editorView.getEditor()
      if path.extname(editor.getPath()) is '.hbs'
        editor.whatGoesHere(HTML) # <= This part?


I think it should be ::setGrammar(grammar). Did you try it?

Check the API Documentation for details


How did you end up doing it? I’m having trouble on how to load grammers atm :confused:


I’m afraid I haven’t been able to wrap my head around grammars either. To be fair though, I haven’t had time to fiddle with it much.


@eriklebsen check this package, and how they do it.


@AbeEstrada: It looks like setGrammarOverrideForPath() needs absolute paths (at least, I couldn’t get wildcards to work).


@eriklebsen: As @DiogoAndre said, setGrammar() is the method you use to set the grammar for a particular editor.

The grammars are all keyed to their Scope name. The Scope for the language-html package is “text.html.basic” (you can check it in the preferences).

You can grab grammars from atom.syntax. Here’s a simple example (see also 2650#4 for more details):

path = require 'path'

atom.workspaceView.eachEditorView (editorView) ->
  editor = editorView.getEditor()
  return unless path.extname(editor.getPath()) is '.hbs'
  htmlGrammar = atom.syntax.grammarForScopeName 'text.html.basic'
  return unless htmlGrammar?
  editor.setGrammar htmlGrammar


I made the file-types package. Check it out if you get a chance.

  path = require 'path' 

  atom.workspaceView.eachEditorView (editorView) ->
    editor = editorView.getEditor()
    editorPath = editor.getPath()
    ext = path.extname(editorPath)
    if ext is '.rb'
      if /app\/(controllers|helpers|mailers|models)/.test(editorPath) || /db\/(migrate|seeds)/.test(editorPath)
        editor.setGrammar atom.syntax.grammarForScopeName('source.ruby.rails')
    else if ext is '.erb'
      if /app\/views/.test(editorPath)
        editor.setGrammar atom.syntax.grammarForScopeName('text.html.ruby')