I\'m using linux and gvim/vim as external editor for matlab.
Using matlab editor you can run a file by pressing F5. I\'m trying to reproduce this with gvim-gtk(offic
I do this on Windows using Autohotkey (which I also use to swap the Esc and Caps Lock keys). Autohotkey can be installed without admin rights, useful on corporate machines.
Using the set up described below:
Cell mode could be achieved through a slightly different mapping for CTRL-E (see below). (The Autohotkey script will execute in Matlab whatever is put into the clipboard by the CTRL-E mapping in gvim.) You can then set up Matlab and gvim side by side or on different screens, and it's a great way to work.
In my Autohotkey file, I use this mapping:
^l::
; execute line or current selection in Matlab
IfWinActive, ahk_class Vim
{
; CTRL-E is mapped to a Vim shortcut to yank the text you want
Send, ^e
WinActivate, ahk_class SunAwtFrame
Send, ^0^v{Enter}
WinActivate, ahk_class ahk_class Vim
} else if WinActive, ahk_class SunAwtFrame
{
Send, ^0^v{Enter}
}
Return
Then, in vim I use the following shortcut:
:nnoremap ^E Y
:vnoremap ^R y
Note: ^E is a a literal CTRL-E entered in vim by pressing CTRL-V CTRL-E or CTRL-Q CTRL-E.
Note 2: SunAwtFrame
is what Windows calls the Matlab window on my machine. You can use Windowspy (which comes bundled with Autohotkey) to confirm that it is called the same on your machine.
EDIT: See plugin made from it: https://github.com/elmanuelito/vim-matlab-behave
Sorry for replying to my own question. It's my bad, I was using xclip where I should have used xclip -selection c . I put below the different codes that work out for me: it reproduces the F5 and the evaluate cell behavior but you have to press Ctrl-V or use the mouse to paste in Matlab Command Window. If you are not on the command window, use matlab shortcut Ctrl-0 to jump to it. The script automatically goes from vim to matlab(unless you have another window with the name "matlab" in it). I added marks to go back to the former cursor position afterwards.
To run the whole script matlab: This is the "Run/F5" behavior (goes to the right directory and execute the whole script):
function! MatRun()
normal mm " mark current cursor in mark m"
let @+="cd('".expand("%:p:h")."\'); run('./".expand("%:f"). "')"
call system('xclip -selection c ', @+)
call system('xclip ', @+)
normal `m "go back to cursor location"
!wmctrl -a MATLAB
endfunction
map ,m :call MatRun() <cr><cr>
To run, evaluate the cell only : This is the Ctrl+Enter behavior. In the same line than above: (edit: I now extended it so that it will work from the first upward %% OR the start of the file (\%^) till the first downward %% OR the end of the file (\^$) end edit)
function! MatRunCell()
normal mm "remember cursor"
:?%%\|\%^?;/%%\|\%$/w !xclip -selection c "pipe the cell to xclip"
normal `m "go back to cursor location"
!wmctrl -a MATLAB "go to matlab window"
endfunction
map ,k :call MatRunCell() <cr><cr>
But I like better the following one, though more complicated (and could certainly be made vim-only). The following, ensures you are in the right directory to run the cell, and also goes back to Vim after evaluating the cell (if you have correctly configured the external editor in matlab. I use:gvim --servername MAT --remote-tab-silent).
function! MatRunCellAdvanced()
execute "!echo \"cd(\'".expand("%:p:h")."\')\">/tmp/buff"
normal mm
:?%%\|\%^?;/%%\|\%$/w>> /tmp/buff
execute "!echo \"edit ".expand("%:f")."\">>/tmp/buff"
!cat /tmp/buff|xclip -selection c
!cat /tmp/buff|xclip
normal `m
!wmctrl -a MATLAB
endfunction
map ,n :call MatRunCellAdvanced() <cr><cr>
Run current line : Copy current line to clipboard and go to matlab window. (once again Control+V to paste it in matlab, if it's the short-cut you use in matlab)
function! MatRunLine()
" write current line and pipe to xclip "
:.w !xclip -selection c
!wmctrl -a MATLAB
endfunction
map ,l :call MatRunLine() <cr><cr>
Run selection : Emulates F9 behavior.
function! MatRunSelect()
normal mm
!rm -f /tmp/buff
:redir > /tmp/buff
:echo @*
:redir END
execute "!echo \" \">>/tmp/buff"
execute "!echo \"edit ".expand("%:p")."\">>/tmp/buff"
!cat /tmp/buff|xclip -selection c
normal `m
!wmctrl -a MATLAB
endfunction
Bold Cell title Bold font in the cell title
highlight MATCELL cterm=bold term=bold gui=bold
match MATCELL /%%.*$/
Where to put those commands All of these commands in .vim/after/ftplugin/matlab.vim
edit Instead of using:
run('./".expand("%:f")."')"
I now use
run('".expand("%:p")."')"
To deal with the case where vim 'pwd' is different from the script directory. I didn't find a way to extract just the script name, so I use the full path. Somebody can probably find a nicer solution for that.
For those of you who just want to be able to run matlab scripts from vim and do not need the other fancy GUI stuff you could try using matlab's command line shell.
:! matlab -nosplash -nodesktop -r %:r
will start a new matlab shell and run the current file as script (or function with no parameters) similar to what F5 in the matlab GUI does. You will need to type 'exit' to get out of the matlab shell and back to vim afterwards. And of course this works only if the file you want to run is in the matlab path or the terminal's working directory.
This may be a bit inconvenient and since a new matlab instance is started all the time, running the script may take a while. You could try using -nojvm
to avoid the overhead caused by java but the best solution in my opinion is keeping the matlab shell open.
You can do this with a Vim Plugin called Conque-Shell.
Once installed you can start the matlab shell with :ConqueTerm matlab -nodesktop
and you will get a buffer that you can use like the matlab console.
Executing code parts will work just fine with yank and paste. Or you could create vim functions that do this for you. Since I'm not very good at this I can only provide this ugly mapping here:
:nmap <f5> :let @"=expand('%:r')<cr>:buffer matlab\ -nodesktop<tab><cr>""p<cr>