如何在Vim中模拟Matlab的运行命令(F5)? [英] How to emulate Matlab’s Run command (F5) from within Vim?

查看:70
本文介绍了如何在Vim中模拟Matlab的运行命令(F5)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Linux和gVim/Vim作为Matlab的外部编辑器.

I’m using Linux and gVim/Vim as an external editor for Matlab.

在Matlab编辑器中,您可以按 F5 运行文件.我正在尝试在Vim(从 gvim-gtk Debian软件包安装)中重现此内容.

In Matlab editor you can run a file by pressing F5. I’m trying to reproduce this in Vim (installed from the gvim-gtk Debian package).

我知道可以通过"shell模式"将名称或当前文件或选定的文本发送到Matlab,如下所示:

I know it is possible to send the name or the current file or the selected text to Matlab in "shell mode" like so:

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 is the solution I thought could work out for me:

  1. 在vim中使用键映射,这将:

  1. Use a key mapping in vim that would:

  • (a)将类似以下内容的内容放入剪贴板: run('path-to-file') cd('folder'); run('filename').
  • (b)调用将焦点放在Matlab GUI上的shell命令

这时,我应该在Matlab命令窗口中,因此只需按 Ctrl - V ,然后粘贴剪贴板的内容.(即使我在Linux上,我也将Matlab与Windows键盘绑定一起使用.)

At this point, I should be in Matlab command window, so I just press Ctrl-V, and the content of the clipboard should be pasted. (Even though I’m on Linux, I use Matlab with Windows keybindings.)

步骤1(a):下面将把 run('filename')命令放到剪贴板上,当按下 Ctrl - V 时,Matlab将其粘贴:/p>

Step 1(a): The following will put the run('filename') command to the clipboard, and Matlab will paste it when Ctrl-V is pressed:

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

以下两个命令可与Linux中间按钮粘贴一起使用,但这不是我想要的,因为我想避免在此步骤中使用鼠标:

The following two commands would work with the Linux middle button pasting, but that is not what I want, as I want to avoid the mouse in this step:

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

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

步骤1(b):要将焦点移至Matlab窗口,请使用以下命令:

Step 1(b): To give the focus to the Matlab window, I use the following command:

wmctrl -a MATLAB &

这很好用,但是,如果碰巧打开了一个页面的标题带有Matlab字样的页面(例如您当前正在阅读的页面),它也可能会引发浏览器窗口.

This works well, but it might also raise a browser window, if it happens to have a page open with the word Matlab in its title (like the one you are reading at the moment).

另请参阅更复杂的解决方案:

See also a more complex solution: Is there a linux command to determine the window IDs associated with a given process ID?.

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

My (now old) option, but Ctrl-V does not work! Only mouse pasting does:

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 the edit below). The whole point for me is to avoid using the mouse for this operation.

谢谢您的帮助.

我不好,我使用的是 xclip ,而我本应该使用的是 xclip -selection c .请在下面查看我的回复.

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 browser windows 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

粗体单元格标题单元格标题中的粗体

 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的运行命令(F5)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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