This is a collection of code sourced from a few places, modified, expanded and put in one place for my own use.
It is my attempt at making vim and matlab work together as painlessly as possible. Some things are still a work in progress.
- Fabrice Guy's syntax highlighting, matchit pairing of for/end, if/end etc, and indentation rules.
- Hitting enter in vim on a word to open it in the variable editor or in vim, if it is a variable or m file.
- Matlab's own keybindings for running code lines, sections, selection, script etc, using tide
- Debugging keybindings, including setting and clearing markers and navigating the function stack
- Automatic session saving like in matlab
- Automatic/on demand code checking and error highlighting with Thomas Ibbotson's mlint.vim
- A shell script to find .m files in specified local directories and add them to the list of functions for syntax highlighting
Most of the cool stuff requires vim be run in server mode. To run a vim server that can use all the functions here, including automatic session restoration, open it with:
vim --servername vimlab
To enable session saving and restoration, add this to your .vimrc:
let g:vimlab_session = "~/.cache/vim/sessions/matlab-session.vim"
changing the path to where you keep vim sessions, and open vim with:
vim --servername vimlab -S $HOME/.cache/vim/sessions/matlab-session.vim
You can change the default binding to save and exit (which is XA in normal mode):
nnoremap XA <Plug>VimlabEndSession
Opening variables, functions or scripts from vim*
Hitting enter when the cursor is on a word will either:
- open it in the variable editor if it is a variable, or
- open it in the vim server if it is a script or function and on matlabs path.
This requires the included op.m function be on matlab's path.
The default bindings are to open the word in the vim server and to open it in a new window, if the word points to an .m file. The vim server name and terminal can be changed from the default "vimlab" and urxvtc with:
let g:vimlab_server = "vimserver" let G:vimlab_terminal = "xterm"
|\a||add a debug marker at the current line (this saves the buffer)|
|\r||remove the marker at the current line|
|\t||toggle the marker at the current line|
|\s or F10||step to the next line in debug mode|
|\c or F5||continue running the code|
|\e||toggle stopping to debug at errors|
|\x||exit debug mode|
|\f||flush all markers|
|\ds||list the debug stack|
|\du||go up the stack|
|\dd||go down the stack|
These keybindings assume the leader key is the default backslash.
This plugin contains Thomas Ibbotson's mlint.vim. If mlint isn't on your system's $PATH, you can set the path by putting this in your .vimrc:
let g:mlint_path_to_mlint = "$HOME/applications/MATLAB/R2018b/bin/glnxa64/mlint"
This will automatically run mlint when the cursor is idle, but this can be disabled by setting this variable to anything:
let g:mlint_hover = 1
Auto linting can be toggled with the ToggleLine command, which is bound by default to
|\ll||run mlint manually|
||||toggle automatic mlint aucommands|
|\lm||see the message from the current line|
|\lo||view an outline of all messages|
|\ln||jump to the next message|
|\lp||jump to the previous message|
Auto close of for, if, switch statements
For, if, and switch statements will automatically get an end placed below them on the next empty line. So if there is a code block on the next line, the statement will surround it. To disable this altogether, set the variable g:vimlab_no_autoend
For matchit word pairing of things such as if/end, allowing the use of % to navigate these pairs, add the following to your .vimrc: