Is there a way to set Sublime Text 2 (running under OSX) so that it can identify MATLAB (.m) files and Objective-C (.m) files automatically? Meaning that ST2 would be able to differentiate between each one and apply it's special syntax highlighting rules. I have already seen this behavior in the all mighty VIM and Emacs.

  • How does VI do it? Is it reading embedded comments for hints? – Ian C. Mar 28 '12 at 5:32
  • I seem to recall that Text Mate would be able to differentiate between those two files using a "first line hint" %matlab at the beginning of all your MATLAB files. There are a few rules that a file could be subjected to to test it's origin. I don't really know how does VI do it but it freaking works :O ... problem is I'm beginning to like ST2. – El Developer Mar 29 '12 at 1:36
  • I can't find any way to give ST2 hints. Cmd-Shift-P to bring up the pallet and then type Set Syntax and pick the syntax is all I've got, sorry. – Ian C. Mar 29 '12 at 1:47

You may want to have a look at /Users//Library/Application Support/Sublime Text 2/Packages/Matlab.sublime-package. It's a simple ZIP file containing a bunch of XML (Plist) files.

In the Sublime Doc for syntax definitions you'd be able to find a documentation of how to create (and edit) syntax definitions.

For an example have a look at the perl syntax definition, it contains a key named "firstLineMatch" which is not used in the Matlab syntax definition. But you could add one and create a file magic for/in your Matlab files.

Update : I just gave it a go and it doesn't seem to work. Do be fair TextMate can't do it either.

Update 2 : After some more investigation: The question about firstLineMatch came up in the Sublime Forum. The file extension is taking precedence over firstLineMatch right now. So there's not much that can be done. With firstLineMatch working you could do nice things. And this would mean that no file magic would be necessary since the syntax of Obj-C and Matlab files is different.

  • Nice bit of sleuthing. – Ian C. Apr 1 '12 at 15:10
  • Yeah, I know that TextMate cannot do this but oh well, I guess I will have to wait for an update and see if this ever gets fixed, because I really hate when those two files get messed up. I currently have it default to highlight the MATLAB's syntax but every now and then I jump and see some Objective-C files outside from Xcode. Because Xcode is a mess when opening external files whilst working in a project. – El Developer Apr 5 '12 at 21:36

In the file Matlab.tmLanguage change it to

    <!-- Actually, it's generally .m, but that's taken by Objective-C. It needs an entry to show up in the syntax list. -->

and remove it from ObjC.tmLanguage

  • Hello Charley, I did this though I didn't see any difference. Are you able to see this working? Are you able to open MATLAB and Objective-C files with their proper colorings? thanks – El Developer Jul 4 '12 at 4:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .