52

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.

Community
  • 1
  • 1
scharfmn
  • 3,561
  • 7
  • 38
  • 53
  • 3
    possible duplicate of [Set default syntax to different filetype in Sublime Text 2](http://stackoverflow.com/questions/7574502/set-default-syntax-to-different-filetype-in-sublime-text-2) – mmmmmm Oct 23 '12 at 14:53

4 Answers4

65

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

Community
  • 1
  • 1
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • 2
    The post that @Eric Leschinski linked to has the answer. Or at least this is the way I set Sublime 2 to associate my .m files. "Open all with current extension as ->" – AGS Aug 23 '12 at 00:24
  • 42
    That's it: View -> Syntax -> Open all with current extension as... -> -- many thanks to all. – scharfmn Aug 23 '12 at 18:05
3

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)
Basj
  • 41,386
  • 99
  • 383
  • 673
2

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.

carandraug
  • 12,938
  • 1
  • 26
  • 38
  • I love Free Software as much as the next guy (or even a bit more), but in this case it doesn't give you an advantage over ST2. You can change the these settings from the ObjC and MATLAB language files to get highlighting to match `.m` (which you have to do in FS as well, if it can be done without recompilation). – Egon Aug 23 '12 at 04:58
  • They are files, and `source()` is very neat. Thank you very much. – scharfmn Aug 23 '12 at 18:25
0

This worked fine for me. I had just put the path for the octave and the code is perfectly running

{
"cmd": ["/usr/local/octave/3.8.0/bin/octave-3.8.0", "$file"],
"selector": "source.m"
}

Save it as Octave.sublime-build