Auto-detect Markdown as language for .txt files?


#1

Dear all,

Can Atom auto-detect and set Markdown as the language for .txt files that contain Markdown markup?

Thankfully,

-Robert


#2

There isn’t a way to do exactly what you’re asking built-in. But you could use this feature to assume that .txt files are Markdown and you could manually change files that aren’t back to plain text:

I think the hard part in what you’re asking is determining whether a block of text is Markdown or not. For example, is this Markdown?

# Some code to do some stuff

a = b*c*d

Or is this Markdown?

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim
id est laborum.

Highlighting syntax for various languages in .html files
#3

Thanks for your elaborative post, @leedohm!

I do see the point that it’s potentially impossible to achieve what I had asked.

I will continue to switch manually.

Warmly,

-Robert


#4

If it’s something that you do all the time, it would be child’s play to set it up as a keybinding. You can add the following to your init.coffee. If you’re using @burodepeper’s language-markdown package, then replace source.gfm with text.md.

atom.commands.add 'atom-text-editor', 'user:switch-to-markdown', ->
  return unless editor = atom.workspace.getActiveTextEditor()
  
  editor.setGrammar(atom.grammars.grammarForScopeName('source.gfm'))

Then in your keymap, you can add something like this.

'atom-text-editor':
  'ctrl-m': 'user:switch-to-markdown'

You can also set up a toolbar with this command as a convenient button.


Change Default Syntax for All New Unsaved Documents
#5

I had a similar suggestion for @orschiro : )


#6

I already thanked @burodepeper and now I want to thank you personally as well for this great solution. Thank you @DamnedScholar! :slight_smile:

-Robert