File Exchange

image thumbnail

Editing Matlab files in Vim

version 1.4 (12.6 KB) by

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

4.46154
16 Ratings

51 Downloads

Updated

View License

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

Comments and Ratings (45)

useruser

adding

setlocal sw=4
setlocal ts=4

in ftplugin/matlab.vim
would mimic the default 4-spaces tab of a matlab file

useruser

Nice plugin

Jianhui Liu

Vaclav Tvrdik

Domenic, same config here on my machine, same issue. I believe it's because files are created on windows and have DOS line endings. I converted them to unix by dos2unix(have this utility installed on my machine), but there is plenty other ways to do it. After conversion errors gone.

I am getting the following error in the ftplugin/matlab.vim file when I open a file in vim.

"problem1_error.m" [noeol] 16L, 312C
Error detected while processing /Users/ddelnano/.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

I am on a mac with the newest version of matlab (R2015a). Anyone run into this issue?

Bergercookie

Great plugin.. solution by @Igor does work perfectly!

Nico Schlömer

Nico Schlömer (view profile)

The indentation is broken for switch statements (`end` is not properly indented).

Igor

Igor (view profile)

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.

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

Ayush

Ayush (view profile)

Alex

Alex (view profile)

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?

Alexis

Alexis (view profile)

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)

john

john (view profile)

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).

Ben

Ben (view profile)

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

Robert

Robert (view profile)

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

Ben

Ben (view profile)

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.

Ben

Ben (view profile)

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.

Geoff

Geoff (view profile)

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

Geoff

Geoff (view profile)

Robert

Robert (view profile)

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!

Fabrice

Fabrice (view profile)

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)

Doug

Doug (view profile)

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!

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!

Carlos

Carlos (view profile)

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.

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?

Fabrice

Fabrice (view profile)

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.

bitozoid

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

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

bitozoid

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

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>

Bartosz

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

David

David (view profile)

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 :).

d c

d c (view profile)

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.

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?

Fabrice

Fabrice (view profile)

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.

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?

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.

Fabrice

Fabrice (view profile)

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.

Jader

Jader (view profile)

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

Ken Lin

thank you for sharing

Yakun Sun

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

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.

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?

James

James (view profile)

Thanks for the updated syntax highlighting.

Updates

1.4

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.

1.3

- 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)

1.2

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

1.1

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

MATLAB Release
MATLAB 7.6 (R2008a)
Acknowledgements

Inspired: Highlighting Matlab files in Vim

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video