File Exchange

image thumbnail

Editing Matlab files in Vim

version (12.6 KB) by Fabrice
Edit Matlab M-files in Vim editor (indentation, syntax highlighting, tags , mlint support)


Updated 19 May 2010

View Version History

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

Cite As

Fabrice (2021). Editing Matlab files in Vim (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (51)

Hao Wu

very nice plugin!

Kris Ezra

The instructions didn't work and no amount of shuffling seemed to autoload the .vim files. Besides specifying the rtp to point to each of these files, the best I could do was to find and source all the .vim files in my .vimrc. That of course works, but it's a little clunky imo.

Unfortunately, the doc is a little light, so I'm missing out on basically all the features but the indent I think... It would be great if someone wanted to port this to Vundle or something and perhaps write up the vim help docs ;).


Anthony, look in doc/matlab.txt for a description and installation instructions. Unless you prefer to use vim over Matlab's built-in editor, you won't find these files useful.

Anthony Castellano

I'm sorry ... how do you use this? Could you put a README file?

Jeremy Rouot

Roscoe Cook

Vaclav Tvrdik,
Thanks! I had the same issue as you and Domenic and your suggestion (dos2unix) fixed it.



setlocal sw=4
setlocal ts=4

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


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.

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?


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

Nico Schlömer

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


mlint in os x:

go to /usr/bin

sudo ln -s /Applications/ mlint

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


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

source $VIMRUNTIME/macros/matchit.vim




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?



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)


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


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.



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


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.


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.


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



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!


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)


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!

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!



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?



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?


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.


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


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


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


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>


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


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

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.

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?


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.


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?

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.


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.


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.

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?


Thanks for the updated syntax highlighting.

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: Highlighting Matlab files in Vim

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!