Vim和Matlab GUI-使用Vim模拟matlab运行(< F5>) [英] Vim and matlab GUI - Emulate matlab Run (<F5>) with Vim

查看:77
本文介绍了Vim和Matlab GUI-使用Vim模拟matlab运行(< F5>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用linux和gvim/vim作为matlab的外部编辑器.

I'm using linux and gvim/vim as external editor for matlab.

使用matlab编辑器,您可以按F5键运行文件.我正在尝试使用gvim-gtk(官方的debian软件包)重现此内容.

Using matlab editor you can run a file by pressing F5. I'm trying to reproduce this with gvim-gtk(official debian packages).

我知道可以在"bash模式"下将文件名或选择内容定向到matlab,如下所示:

I know it is possible to direct filename or selection to matlab in "bash mode" like the following:

execute '!echo "' ."run(\'".expand("%:p")."\')" . '"| matlab -nojvm' 
execute '!matlab -nodisplay < '.expand("%:p") 
noremap <C-CR> :?%%?;/%%/w !matlab -nojvm

但是我想使用Matlab gui(在我的情况下已经打开了). 这是我认为可以为我解决的解决方案:

But I want to use the Matlab gui (which in my case is already opened). Here's the solution I thought could work out for me:

  1. 在vim中使用密钥映射将:
    • (a)在剪贴板中放置以下内容:run('path-to-file')或cd('folder'); run('filename')
    • (b)调用一个shell命令,该命令将焦点集中在matlab GUI上
  1. Use a key mapping in vim that would:
    • (a) put in the clipboard something like: run('path-to-file') or cd('folder');run('filename')
    • (b) call a shell command that gives the focus to the matlab GUI

步骤1a: 下面将把run('filename')命令放到剪贴板上,matlab会在按下CTRL-V时粘贴它

Step 1a: The following will put the run('filename') command to the clipboard, and matlab will paste it when pressing CTRL-V

let @+="run(\'".expand("%:p")."')" 

下面的两个将与linux中键粘贴一起工作(但这不是我想要的,因为我想在此步骤中避免使用鼠标)

The two following will work with the linux middle button pasting(but that's not what I want since I want to avoid the mouse in this step)

let @*="run(\'".expand("%:p")."')" 

execute '!echo "' . "run(\'".expand("%:p")."\')" . '"| xclip'  

步骤1b:要将焦点移到matlab窗口,请使用以下命令:

Step 1b: To give the focus to matlab window I use the following command:

wmctrl -a MATLAB &

这很好用,但如果您在标题中包含matlab字样的页面上(例如正在阅读的内容...),它也会看到您的浏览器. (另请参见更复杂的解决方案

This works well, but it will also see your browser if you are on a page with the word matlab within the title(like the one you are reading...). (see also a more complex solution Is there a linux command to determine the window IDs associated with a given process ID? )

我的(现在是旧的)选项,但是CTRL-V不起作用!仅鼠标粘贴:

function! MatRun()
    let @+="run(\'".expand("%:p")."')" 
    let @*="run(\'".expand("%:p")."\')" 
    :call system('xclip', @+)
    !wmctrl -a MAT
endfunction

map <F5> :call MatRun() <cr><cr>

以某种方式,当与wmctrl结合使用时,CTRL-V在这种情况下不起作用(请参见下面的编辑).对我而言,最重要的是避免使用鼠标进行此操作.

Somehow, CTRL-V does not work in this case when combined with wmctrl (see edit below). The whole point for me is to avoid using the mouse for this operation.

谢谢您的帮助.

编辑和工作选项 不好的是,我在使用xclip的地方应该使用xclip -selection c.请在下面查看我的回复.

Edit and working option My bad, I was using xclip where I should have used xclip -selection c. See my reply below..

如果有人找到更好的解决方案,我会很高兴,例如:直接粘贴到matlab命令窗口中,确保您位于命令窗口中(Ctrl-0),或者避免使用wmctrl捕获浏览器.

I'd be glad if somebody find a better solution, for instance: pasting directly in matlab command windows, making sure you are in the command windows (Ctrl-0), or avoiding catching the browser with wmctrl.

推荐答案

查看由它制成的插件: https://github.com/elmanuelito/vim-matlab-behave

See plugin made from it: https://github.com/elmanuelito/vim-matlab-behave

很抱歉回答我自己的问题.这很不好,我使用xclip,而应该使用xclip -selection c. 我在下面列出了适合我的不同代码:它可以再现F5和评估单元格的行为,但是您必须按Ctrl-V或使用鼠标粘贴到Matlab命令窗口中.如果您不在命令窗口中,请使用matlab快捷键Ctrl-0跳转至该窗口.该脚本会自动从vim转到matlab(除非您有另一个名为"matlab"的窗口).我添加了标记,然后返回到以前的光标位置.

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.

要运行整个脚本,请执行以下操作::这是运行/F5"行为(转到正确的目录并执行整个脚本):

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>

要运行,请仅评估单元格:.这是Ctrl + Enter行为.在与上面相同的行中:( edit :现在,我对其进行了扩展,以便它将从文件的第一个向上的%%或文件的开头(\%^)到第一个向下的%%或文件末尾(\ ^ $)结束编辑)

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>

但是我更喜欢下面的内容,尽管比较复杂(当然可以将其设置为仅用于vim). 以下内容可确保您在正确的目录中可以运行该单元,并且在评估该单元之后还可以返回到Vim(如果已在matlab中正确配置了外部编辑器.我使用:gvim --servername MAT --remote-tab -沉默的).

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>

运行当前行: 将当前行复制到剪贴板,然后转到Matlab窗口. (如果这是您在matlab中使用的快捷方式,请再次按Control + V将其粘贴到matlab中)

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>

运行选择:模拟F9行为.

  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 /%%.*$/

将这些命令放在何处 .vim/after/ftplugin/matlab.vim

Where to put those commands All of these commands in .vim/after/ftplugin/matlab.vim

修改 而不是使用:

 run('./".expand("%:f")."')"

我现在使用

run('".expand("%:p")."')"

要处理vim'pwd'与脚本目录不同的情况.我没有找到只提取脚本名称的方法,所以我使用完整路径.也许有人可以找到一个更好的解决方案.

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.

这篇关于Vim和Matlab GUI-使用Vim模拟matlab运行(&lt; F5&gt;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆