Dav*_*art 7 vim editor cell ipython-notebook
在IPython笔记本中使用键盘快捷键会很棒,这样可以在外部编辑器(例如gvim)中编辑当前单元格的内容.也许只需将当前单元格的内容复制到临时文件中,在其上启动gvim,并在每次保存文件时更新当前单元格(并在退出gvim时删除临时文件).此外,如果从浏览器编辑单元格,也可以更新临时文件,以便gvim知道文件已更改.
我知道像vim-ipython和ipython-vimception这样的项目,但它们并不符合我的需求.我认为浏览器足以满足简单的需求,但是当需要更强大的编辑功能时,无需重新发明轮子.
你知道IPython笔记本中是否存在这样的功能吗?
谢谢.
Dav*_*art 11
这就是我提出的.我添加了2个快捷方式:
因此,当您想使用首选编辑器编辑单元格时,点击"g",对单元格进行所需的更改,将文件保存在编辑器中(并退出),然后点击"u".
只需执行此单元格即可启用以下功能:
%%javascript
IPython.keyboard_manager.command_shortcuts.add_shortcut('g', {
handler : function (event) {
var input = IPython.notebook.get_selected_cell().get_text();
var cmd = "f = open('.toto.py', 'w');f.close()";
if (input != "") {
cmd = '%%writefile .toto.py\n' + input;
}
IPython.notebook.kernel.execute(cmd);
cmd = "import os;os.system('gvim .toto.py')";
IPython.notebook.kernel.execute(cmd);
return false;
}}
);
IPython.keyboard_manager.command_shortcuts.add_shortcut('u', {
handler : function (event) {
function handle_output(msg) {
var ret = msg.content.text;
IPython.notebook.get_selected_cell().set_text(ret);
}
var callback = {'output': handle_output};
var cmd = "f = open('.toto.py', 'r');print(f.read())";
IPython.notebook.kernel.execute(cmd, {iopub: callback}, {silent: false});
return false;
}}
);
Run Code Online (Sandbox Code Playgroud)
对于使用 IPython终端应用程序发现此问题的人,有一个内置的键盘快捷键,可以$EDITOR使用当前单元格的内容启动。保存并退出编辑器会将单元格的内容替换(但尚未执行)保存的文件的内容。
默认键盘快捷键是F2键。这对应于 IPython 设置IPython.terminal.shortcuts.open_input_in_editor。