.m files as MATLAB/Octave for Sublime Text

Sublime sees .m files as Objective C files.

I've been given a bunch of MATLAB/Octave files to work with using Octave, all with .m extensions.

If I change the extensions to .matlab, then Sublime will recognize them and format them correctly, but then Octave doesn't see them.

I would like to either:

(1) alter Sublime so that it recognizes .m as MATLAB/Octave instead of Objective C, or

(2) tell Octave to recognize .matlab as something it can just run.

I've poked around on the Sublime and Octave sides of this, but to no avail (this is not the answer), and I'm an Octave noob. Thank you.


Sublime 2 can be configured to associate certain file extensions to certain syntax highlighting schemes. See this answer for exactly how to do it https://stackoverflow.com/a/8014142/694184


Here is how to run Matlab/Octave code directly from Sublime Text with CTRL+B and associate .m files to this Matlab syntax. Here it is specific for Windows, see the reason below in the note.

First a Matlab.sublime-build:

{
    "cmd": ["cmd", "/k", "D:\\Documents\\software\\octave-4.0.0\\bin\\octave-cli.exe", "$file"],
    "selector": "source.matlab"
}

Then a Matlab.sublime-settings:

{
    "extensions":
    [
        "m"
    ]
}

Note: I used the cmd /k trick to have the plot displayed in a new window that doesn't "disappear" after 10 milliseconds (see How to run Octave code without the Octave IDE (similarly to Python)? and Octave: How to prevent plot window from closing itself?). For this reason, don't forget to add this to your plots:

h = plot(...)
waitfor(h)

Are those files scripts or functions? If they are Octave scripts and not functions then you can have any extension you like and use source() to run them, independently of the extension. If they are functions, unlike Matlab you can define functions in a script file, load it, and they will stay in memory.

But if you don't need syntax highlight at all, you should be able to change Sublime, just find the file Objective-C.Sublime-package. Without running it, just by looking at the files in the installer, I see two ways to do it:

  1. try to remove it (or just change its name for something .bak so you can restore it in case something goes wrong)
  2. that file is a tarball with two XML files. Open those files and edit the entry that sets what's the extension for those files for something different of .m

And of course, there's also the option of change to a free as in freedom text editor that allows you to look in their source and do whatever you want.