Change Default Syntax for All New Unsaved Documents


#1

I’ve searched extensively and found many articles that sound similar but are subtly different from what I’m looking for.

When opening a file such as index.html atom automatically knows to style the syntax properly, great. Or if I create a document and save it as say test.html it automatically knows to change the syntax to be HTML, great. However, the following is my issue…

Often I paste in code into Atom to quickly edit something and then copy and paste that code back somewhere else. For instance editing code from MailChimp or countless other places. The problem is when you open up atom it defaults to “Plain Text” and if you never actually save the file it will never change from Plain Text and instead each time you have to manually click Plain Text at the bottom and select the language of choice to get proper syntax highlighting.

What I would like to do is change the default Plain Text to say HTML or PHP. And of course if when I save the article as .js or something then like normal it would update to javascript syntax. Does anyone know how to change the default syntax from Plain Text to PHP either via ideally the native config file or if not available via a plugin? Or if you are aware of any help document that outlines this already would be grateful if you could point me to it. Thank you!


#2

We have a FAQ on this here:


#3

Thanks, you rock!


#4

@leedohm

Just tried this out but can’t seem to get it to work. Tried to reply to that message you linked to but doesn’t seem to allow comments so will post here.

Opened ~/.atom/init.coffee and pasted in your above code. I changed, “source.shell” to “source.php” so the new default for all documents would be PHP syntax. I saved the coffee file and restarted atom. Opened a new blank document and it still appears to be defaulting to Plain Text syntax. Am I missing something?


#5

I belive the grammar name is text.html.php for php files and not source.php. I got this information by running atom.grammars.selectGrammar('x.php').scopeName in the console.

Here is what I added to my init.coffee before restarting Atom to test. Opening a new untitled document changed the syntax in the bottom right corner to PHP.

atom.workspace.observeTextEditors (editor) ->
  original = editor.getGrammar()
  if original? and original is atom.grammars.grammarForScopeName('text.plain.null-grammar')
    editor.setGrammar(atom.grammars.grammarForScopeName('text.html.php'))

#6

Thanks, that makes sense and glad it’s working on your end, that’s promising. However when copying and pasting that into the init.coffee file, saving the file, then restarting Atom. And opening a new blank file it still defaults to Plain Text.

Checked and I’m using the latest stable version.

Are you are aware of anything else I might need to do, to get the init.coffee to take affect? Such as restarting the whole computer or emptying some sort of cache in Atom? Any changes made to my preference files always take affect right away but haven’t have tried the init.coffee file before.


#7

Restarting Atom is the only thing you need to do in order to make the init.coffee take effect. Keep in mind that CoffeeScript is an indentation-sensitive language. If you change the indentation, you can change the meaning of the code and it may not work.


#8

Thanks for the screenshots it helped clarify what might be happening.

If I open Atom the editor that opens by the Open Empty Editor At Start config seems to be Plain Text syntax. Other editors opened with Ctrl+N use the PHP syntax. Is this maybe what is happening to you? Did you open that empty editor or was it opened automatically when you started Atom?


#9

When opening Atom for the first time, such as clicking in the dock, or via Spotlight or Alfred, the default blank window opens Plain Text. If I then click open new window it also defaults to Plain Text. However, I can confirm now, that if in one of those windows I choose File > New Tab or Ctrl+N it does default to PHP.

So looks like the issue is that it is not defaulting new windows or the intial window to the desired syntax. Not sure if anyone is aware of any code to resolve this issue, or a package, or if something that will need to be resolved/changed in the Atom Core update in the future to make it possible? Thanks!


#10

Used the indentation provided by you and others (just copied and pasted) but it looks like Ben3eeE is able to replicate that it only works when doing a new tab and not the default window or new window as desired.


#11

I suspect that the initial file is opened before init.coffee gets a chance to run.


#12

Yeah so sounds like the solution posted here, and on GitHub and on the Atom FAQ won’t work. Hopefully someone here or on GitHub will have another potential solution on how to resolve it via a preference setting, or a plugin, or if neither are possible open an an issue with developers to have that updated in a future Atom release.


#13

It will work. You’ll probably just have to change the conditional to ask if getTitle() is untitled.


#14

Thanks @Damned. Could you provide suggested sample code on how that would work?

Currently trying the official version on the Atom Forum FAQ as well as the ones suggested in this thread such as:

atom.workspace.observeTextEditors (editor) ->
  original = editor.getGrammar()
  if original? and original is atom.grammars.grammarForScopeName('text.plain.null-grammar')
    editor.setGrammar(atom.grammars.grammarForScopeName('text.html.php'))

Something like this?..

atom.workspace.observeTextEditors (editor) ->
  original = editor.getGrammar()
  if original? and original is atom.grammars.grammarForScopeName('text.plain.null-grammar') and getTitle() is untitled
    editor.setGrammar(atom.grammars.grammarForScopeName('text.html.php'))

Will try that and a few variations when I’m back at my desk and not on my phone, or if you or anyone has recommendation, that would be great. Thanks so much.


#15

It would look like this:

atom.workspace.observeTextEditors (editor) ->
  if editor.getTitle() is "untitled"
    editor.setGrammar(atom.grammars.grammarForScopeName('text.html.php'))

But I tried it and it’s the same problem. observeTextEditors should catch all currently open editors on its first run and then all new editors, but no dice.


#16

Yeah, this sounds like a bug to me. I would expect it to work on the first editor.


#17

Thanks both. Not sure if one of you knows the best way to report the bug and include the relevant technical info, if you’d suggest I create a GitHub issue including your comments. Thanks either way everyone.


#18

When you create an issue on atom/atom you will get a default issue template, fill out that template completely and include a link to this discussion. Also be sure to read the reporting bugs section of the documentation which has a slightly different template that you can follow or take inspiration from.

Don’t forget to mention that having the Open Empty Editor On Start option enabled is required as this issue only occurs on the first editor created by that option. Also include the full coffeescript snippet that you used.

If you are unsure that creating an issue is a good option from here and that this seems like a valid bug or something we can always wait for @leedohm to reply with his thoughts.


#19

On the more practical side, until the apparent bug is fixed, you have two options. The first is to unset Open Empty Editor on Start, include the init.coffee code, and then just open a new file when you start Atom. The second would be to set up a keybinding that switches the grammar to a language you use all the time. Each of these solutions requires one extra key press on your part, and with the second way, you only have to use the command when you’re working on code that’s long enough for you to want highlighting.


#20

You should follow @Ben3eeE’s great suggestions and when you file the Issue, please post a link to the Issue here in this topic for others to find more easily :grinning: