How to turn on / change language syntax highlighting


#1

Working on a html.twig file (have seen this happen in other files too) and Atom isn’t highlighting any of the language syntax, is there a way to force a specific language to be used i.e. PHP or html etc…?


#2

To temporarily change the grammar, hit ctrl-shift-L and select the grammar you’d like to use.

To permanently change the grammar for a certain file extension, add the following to your init script (init.coffee):

path = require 'path'

atom.workspaceView.eachEditorView (editorView) ->
  editor = editorView.getEditor()
  if path.extname(editor.getPath()) is '.twig'
    grammars = atom.syntax.getGrammars().filter (grammar) ->
      grammar.name is 'HTML'
    editor.setGrammar grammars[0]

I suspect there is an easier way to accomplish this but I couldn’t find a simple way to get a hold of the html grammar object.

To find the correct grammar name look at the result of console.table(atom.syntax.getGrammars(), 'name') in the console.


#3

There’s also the file-types package that basically does this already.


#4

Try

grammar = atom.grammars.grammarForScopeName 'scope.html'
editor.setGrammar grammar

#5

There is a FAQ for this now:

The solution is much simpler and more comprehensive.