View License

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

» Watch video

4.5 | 14 ratings Rate this file 53 Downloads (last 30 days) File Size: 12.6 KB File ID: #21798 Version: 1.4

Editing Matlab files in Vim


Fabrice (view profile)


17 Oct 2008 (Updated )

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

| Watch this File

File Information

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


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 (42)
13 Oct 2015 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.

Comment only
31 Aug 2015 Domenic Del Nano

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?

Comment only
20 Jun 2015 Bergercookie

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

05 Nov 2014 Nico Schlömer

Nico Schlömer (view profile)

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

Comment only
20 Feb 2014 Igor

Igor (view profile)

mlint in os x:

go to /usr/bin

sudo ln -s /Applications/ mlint

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

Comment only
15 Oct 2013 Vincent

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

source $VIMRUNTIME/macros/matchit.vim


Comment only
01 Jul 2013 Ayush

Ayush (view profile)

16 May 2013 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?

Comment only
20 Sep 2012 Alexis

Alexis (view profile)

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)

Comment only
15 Jan 2012 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).

11 Aug 2011 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.


Comment only
10 Aug 2011 Robert

Robert (view profile)

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

Comment only
05 Jul 2011 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.

Comment only
05 Jul 2011 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.

29 May 2011 Geoff

Geoff (view profile)

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

Comment only
29 May 2011 Geoff

Geoff (view profile)

20 May 2011 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:


When typed, this looks like:


Save, exit, and everything should work!

21 Mar 2011 Fabrice

Fabrice (view profile)

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 : where you can find the .ctags file)

Comment only
17 Mar 2011 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!

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!

Comment only
01 Mar 2011 Carlos

Carlos (view profile)


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?


Comment only
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?

Comment only
17 May 2010 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.

Comment only
15 May 2010 bitozoid

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

Comment only
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
" No indentation in a subfunction
- if getline(v:lnum) =~ '^\s*\function\>' && s:functionWithoutEndStatement
+ if getline(plnum) =~ '^\s*\function\>' && s:functionWithoutEndStatement
let curind = curind - &sw
" First case after a switch : indent

Comment only
15 May 2010 bitozoid

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

Comment only
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>

Comment only
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?

Comment only
16 Feb 2010 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 :).

Comment only
02 Feb 2010 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.

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?

Comment only
18 Nov 2009 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.

Comment only
15 Nov 2009 Yaroslav

Yaroslav (view profile)

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

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.

Comment only
23 Sep 2009 Jader

Jader (view profile)

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

Comment only
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.

Comment only
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?

Comment only
25 Nov 2008 James

James (view profile)

Thanks for the updated syntax highlighting.

24 Oct 2008 1.1

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

28 Oct 2008 1.2

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

23 Nov 2009 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)

19 May 2010 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.

Contact us