如何在Vim中实现类似MATLAB的单元格模式 [英] How to implement MATLAB-like cell mode in Vim

查看:81
本文介绍了如何在Vim中实现类似MATLAB的单元格模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB中,我们可以将以下内容写入编辑器

In MATLAB, we can write to the editor the following

%% -- a example cell --

plot(rand(3));

%% -- another cell
A=rand(2^10);
t=linspace(-pi,pi,2^10);
compass(fft(A*sin(t))

,我们只需按 Ctrl + Enter 即可运行鼠标指针所单击的单元格.

and we can just hit Ctrl+Enter to run the cell being clicked by the mouse pointer.

现在我在Vim中知道了,我可以做

Now I know in Vim, I can do

:'<,>'w !matlab运行直观选择的代码块.

:'<,>'w !matlab to run a visually selected block of code.

但是如何在Vim/gVim中实现类似MATLAB的单元格模式?

But how do I implement the MATLAB-like cell mode in Vim/gVim?

例如一些python代码

For example some python code

import os
import subprocess
import random

## 1st cell
ps =["python", "-h"]
out = subprocess.Popen(ps).communicate()[0]
print out

## 2nd cell

# import random -sould not be needed if we concatenate the import section and the cell
print random.randint(1,100)

任何人都可以提出一些想法吗?

Can anyone offer some ideas?

推荐答案

不确定要问的是什么,但是如果要用鼠标双击块来处理单元块,则您需要可以双击鼠标(<2-LeftMouse>映射)以调用函数:

Not sure exactly what you're asking, but if what you want is to process a cell block upon double-clicking in the block with a mouse, then you can map mouse-double-click (the <2-LeftMouse> mapping) to call a function:

nnoremap <buffer> <2-LeftMouse> :call ProcessMouseDoubleClick()<CR>

ProcessMouseDoubleClick()将是一个函数,该函数(1)直观地选择单元格"区域,并且(2)发出'<,>'w !matlab以使Matlab运行所选代码.

ProcessMouseDoubleClick() would be a function that (1) visually selects the "cell" area and (2) issues '<,>'w !matlab to have matlab run the selected code.

调用ProcessMouseDoubleClick时,Vim光标将位于您在文档中单击的任何位置.除此之外,鼠标没有什么特别的.因此,您也可以将任何键映射到相同的功能,例如,用于评估单元格"的助记符命令:

When calling ProcessMouseDoubleClick the Vim cursor will be located at whatever point you clicked in the document. Other than that, there's nothing specific to the mouse. So you could also map any key to same function, e.g., a mnemonic command for 'evaluate cell':

map <buffer> <Leader>ec :call ProcessMouseDoubleClick()<CR>

因此,根本没有任何理由要使用函数名称来引用鼠标,您可能想将其命名为EvaluateMatlabCell()之类.

So, there's not really any reason to have function name referencing mouse at all, you might want to call it something like EvaluateMatlabCell().

这篇关于如何在Vim中实现类似MATLAB的单元格模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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