Code covered by the BSD License  

4.46154

4.5 | 13 ratings Rate this file 89 Downloads (last 30 days) File Size: 12.6 KB File ID: #21798

Editing Matlab files in Vim

by

 

17 Oct 2008 (Updated )

Edit Matlab M-files in Vim editor (indentation, syntax highlighting, tags , mlint support)

| Watch this File

File Information
Description

Contains a set of files useful to edit Matlab files.

Included is :
1) Syntax highlighting
2) Correct indentation
3) Integration in vim of the mlint Matlab code checker with the :make command
4) Correct setting to use the matchit.vim script (extension of the % command to match if/end, for/end,... blocks)
5) Tag support
6) Help file

Acknowledgements

This file inspired Highlighting Matlab Files In Vim.

MATLAB release MATLAB 7.6 (R2008a)
Other requirements Vim 7.0
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (38)
20 Feb 2014 Igor

mlint in os x:

go to /usr/bin

run:
sudo ln -s /Applications/MATLAB_R2013b.app/bin/maci64/mlint mlint

Depending on your version of matlab chose different path in Applications.

15 Oct 2013 Vincent

To get matchit to work, you need to add the following to your .vimrc file:

source $VIMRUNTIME/macros/matchit.vim

source: www.vim.org

01 Jul 2013 Ayush  
16 May 2013 Alex

I am having the same problem as many others it seems, as I am getting an error when the ftplugin file is called. How do I fix this? Where do I add the contents into matchit.vim file as leptogenesis did to make the code work?

20 Sep 2012 Alexis  
08 Feb 2012 leptogenesis

Nice job on these plugins! However, I had the same problem as john--I had to add the contents of ftplugin/matlab.vim to matchit.vim to make the ftplugin work on one of my systems (linux).

More importantly, though, the mlint integration seems almost useless because you have to scroll through every stupid warning mlint spits out to find the true errors. Is there a way to make it only report things that would actually prevent the program from running? (e.g. missing 'end' statements, unmatched parens, etc)

15 Jan 2012 john

Matching if/end and for/end blocks did not work for me. I ended up using matchit.vim directly to set these myself, and getting Matlab syntax highlighting as part of a larger set of syntax highlighting for other languages. The installation instructions should be improved/clarified, and possibly fixed, as they don't work on an out-of-the-box vim install (on Mac Snow Leopard).

11 Aug 2011 Ben

Thanks for the reply Robert. I had tried that before with no success. What ended up fixing it was running dos2unix (fromdos in Ubuntu) on the file. :%s/^M/\r/g has always worked for me in the past, so that was why I was confused by this. Thanks for your help.

Ben

10 Aug 2011 Robert

Ben,
See my comment above for how to fix error you're getting.

05 Jul 2011 Ben

Well, I spoke too soon with issue number 2. To limit spellchecking to comments, open syntax/matlab.vim and add ",@Spell" at the ends of lines 57 and 58. To include strings, add "contains=@Spell" to the end of line 36.

This is the same method used in the built in c.vim file. cpp.vim doesn't have this though, so I'm not sure why it does this except that it probably loads the c.vim first.

Anyway, this might be something good to add to future versions. If anyone has a solution to the ftplugin/matlab.vim error I'm getting, I would love to hear it.

05 Jul 2011 Ben

This set of files is great. Matlab files in Vim used to be a huge pain. I am having a couple small issues, though.
First, when I open a .m file, I get this error:
Error detected while processing /home/bbergman/.vim/ftplugin/matlab.vim:
line 5:
E492: Not an editor command: ^M
line 6:
E15: Invalid expression: exists("b:did_ftplugin")^M
line 35:
E171: Missing :endif

Opening the ftplugin file, I see no ^M characters and all endifs seem to be fine. I have not worked with ftplugin files before, so I have no idea what the issue is with the exists line.

My second issue is that spelling and grammar checking flags all sorts of stuff in the code. In contrast, when I open a c file, only text in comments and string literals get checked. I forget if this is something I had to explicitly do for c/c++, but it would be nice to have in here as well.

Third, matching if/endif with % seems to work great, but % will not cycle through elseifs as it does in some other languages. This might be something to add in future versions.

Again, this set of plugins is great and I only wish I had found them a long time ago. Keep up the good work.

29 May 2011 Geoff

Thanks :-) very handy. Has anyone succeeded in getting mlint :make to work under OSX?

29 May 2011 Geoff  
20 May 2011 Robert

This works very well, thank you! One suggestion for linux users:

To make mlint work, symlink mlint into /usr/bin so that it's in the path:

(in /usr/bin)
sudo ln -s <MATLABPATH>/bin/glnx86/mlint mlint

Also, the matlab.vim ftplugin is in DOS format, and vim will complain about "^M" characters. To fix this, open matlab.vim using the following command:

vi -U NONE -u NONE ~/.vim/ftplugin/matlab.vim

then, replace the ^M characters with newlines like this:

:%s/[CTRL-V,CTRL-M]/\r/g

When typed, this looks like:

:%s/^M/\r/g

Save, exit, and everything should work!

21 Mar 2011 Fabrice

Carlos,
You are right, the .ctags is missing in the zip. I will try to upload a new version of the package this week (but you can still download an older version of the zip here : http://www.vim.org/scripts/script.php?script_id=2407 where you can find the .ctags file)

17 Mar 2011 Doug

When I use :make I get the error "/bin/bash: mlint: command not found". I've added the path to mlint.m to my .bashrc. Am I missing something else?

Also, the code above by Bartosz to execute the current file is great. Anybody got code to execute the current selection? As an alternative, I've written a function grablines.m that creates a new (temporary) m-file from a range of lines in an existing file, which is then easy to run. I'll post it if there's interest.

Thanks to Fabrice for a very helpful package!

15 Mar 2011 Pankaj Deshpande

I am also interested in having a way to use tags for matlab files and .ctags will be very helpful but I guess it's missing from the .zip file. Thanks!

01 Mar 2011 Carlos

Fabrice,

Great code! I've been looking for something like this forever.

Quick question: I am missing something, or there should be a .ctags file in the .zip?

Thanks.

14 Sep 2010 Jonathan

This works great. I've been using it with vim63, and just upgraded to vim73. It works with vim73, except when I try the new -p --remote-tab-silent options, the matching doesn't work. Has anyone got the plugin to work with these options?

17 May 2010 Fabrice

For expressions like a(end) (or a(end+1), a(end-1),...) I have done some enhancements regarding the matchit support : I will upload a new version soon.

15 May 2010 bitozoid

I have some expressions like 'a(end)'. The matcher does not seem to work in this case.

15 May 2010 bitozoid

At the moment this works for me:
--- /usr/share/vim/vim72/indent/matlab.vim-orig 2010-05-15 16:07:37.000000000 +0100
+++ /usr/share/vim/vim72/indent/matlab.vim 2010-05-15 16:08:09.000000000 +0100
@@ -116,7 +116,7 @@
let curind = curind - &sw
endif
" No indentation in a subfunction
- if getline(v:lnum) =~ '^\s*\function\>' && s:functionWithoutEndStatement
+ if getline(plnum) =~ '^\s*\function\>' && s:functionWithoutEndStatement
let curind = curind - &sw
endif
" First case after a switch : indent

15 May 2010 bitozoid

I get indentation for a main function (without end). Is this normal behaviour? Can I avoid it?

07 Mar 2010 Bartosz

I got it, just add the lines below to your matlab.vim

" Execute file being edited with F5:
map <buffer> <F5> :w<CR>:!matlab -nodesktop -nosplash -r "try, run %, pause, catch, end, quit" <CR> <CR>

20 Feb 2010 Bartosz

Great plugin! Its just the thing I was looking for.
Is there a way to run the .m scripts directly from vim?

16 Feb 2010 David

This is a very good plug-in, and Fabrice is very good a responding to any problems. I had the same problem with the indenting not working 'out of the box' and the same fix applied to me. I have however made a change to the indenting code to remove the tic; toc; indenting since I use "t=toc;" which isn't picked up. Everything else is very good however. "=G" is my friend :).

02 Feb 2010 d c

I appreciate the .vim plugin immensely. Unfortunately it didn't quite work perfectly for me. Is the new one up yet? If it is I'll check it and post any errors I have.

08 Jan 2010 Arvind Raghavan

I had to add the following line to my _vimrc to get the indenting to work (syntax works "out of the box" with the installation instructions provided):

filetype indent on

Anybody else have this problem, or I am missing something obvious?

18 Nov 2009 Fabrice

I have done enhancements in the indent file to indent correctly one-line blocks. I'll provide the new version of the file as soon as possible.

15 Nov 2009 Yaroslav

Does anybody know what script should I add to the "indent/matlab.vim" file in order to get the following statement (or similar to it) indented correctly:

if (some_conditional), do_something; end;

Unfortunately it matches the "end" to the former statement that required "end" (such as "switch"). But I like to keep my code neat and small.

Any advice?

21 Oct 2009 Krishna Sankar M

Thanks Fabrice. Very useful.
May I request some more features for matching: in addition to existing, support matching groups as if/else, else/end, case/<next>case.

24 Sep 2009 Fabrice

To install it on windows : unzip the package and extract it to your vimfiles folder (usually C:\Program Files\Vim\vimfiles).

For more information about the installation, there is a matlab.txt file provided in the package you could read.

23 Sep 2009 Jader

How to install it on windows if I can't find any .vim folder?

30 Aug 2009 Ken Lin

thank you for sharing

01 May 2009 Yakun Sun

This is splendid. It solves the problem of indenting .m codes that troubles me for a million years.

28 Mar 2009 Ritesh Sood

A handy shell snippet to copy the downloaded files under appropriate dirs in your .vim/
$ cd matlab # the dir you downloaded
$ tar -cf - | (cd ~/.vim; tar -xf -)
You tar the files to stdout, cd to ~/.vim and untar stdin; no intermediate file creation.

01 Mar 2009 Jørgen Skancke

Thanks for the useful files.

In emacs there is a matlab-mode that lets you evaluate selected text in matlab (like in the Matlab-editor, handy for playing with plots and the like). Since this is possible in emacs, it should be possible in vim too. Does anyone have ideas about how to implement this?

25 Nov 2008 James

Thanks for the updated syntax highlighting.

Updates
24 Oct 2008

- added a help file (matlab.txt)
- wrong indentation for subfunctions without matching 'end' has been fixed

28 Oct 2008

Enhanced syntax highligting for Matlab m-files : most Matlab builtin functions are now highlighted

23 Nov 2009

- Added support for indentation of if/end blocks on the same line
- Changed the function indentation behavior (now based on the 'indent all functions' option in the Matlab Editor)

19 May 2010

Updated support for the matchit script (to use % to jump between try/end, if/end, switch/end,... blocks) , to deal with expressions like a(1+end) where 'end' is not the end of a block command but the length of a vector.

Contact us