bro*_*hlr 17 javascript keyboard-shortcuts ipython-notebook
在IPython Notebook环境中,可以使用IPython Javascript API定义自定义键盘快捷键.使用%%javascript魔法,可以在IPython的交互式控制台中编写一个javascript,如下所示(此处描述的示例):
%%javascript
IPython.keyboard_manager.command_shortcuts.add_shortcut('r', {
help : 'run cell',
help_index : 'zz',
handler : function (event) {
IPython.notebook.execute_cell();
return false;
}}
);
Run Code Online (Sandbox Code Playgroud)
我想编写一个javascript,在编辑模式下创建一个快捷方式,将Ctrl-Alt-Down绑定到"重复当前行"的动作 - 也就是说,将光标移动到当前行的开头,选择行,复制行,返回,粘贴.基本上,我想模拟Eclipse的键盘快捷键,或Notepad ++中的Ctrl-d,或Emacs中的Ca C-SPACE Cn Mw Cy.javascript文件将采用以下形式:
%%javascript
IPython.keyboard_manager.edit_shortcuts.add_shortcut('ctrl-alt-down', {
help : 'run cell',
help_index : 'zz',
handler : function (event) {
[Code that duplicates the line];
return false;
}}
);
Run Code Online (Sandbox Code Playgroud)
虽然我的尝试建议'ctrl-alt-down'是表示快捷方式序列的错误方法,但我找不到任何文档keyboard_manager.
我宁愿不使用(例如)AutoHotKey解决方案,因为我想将此快捷方式限制为IPython Notebook的编辑模式.
PAD*_*MKO 18
步骤1.
~/.jupyter/custom/custom.js如果它不存在,则创建一个新的JS文件并添加下一个代码:
/**
*
* Duplicate a current line in the Jupyter Notebook
* Used only CodeMirror API - https://codemirror.net
*
**/
CodeMirror.keyMap.pcDefault["Ctrl-Down"] = function(cm){
// get a position of a current cursor in a current cell
var current_cursor = cm.doc.getCursor();
// read a content from a line where is the current cursor
var line_content = cm.doc.getLine(current_cursor.line);
// go to the end the current line
CodeMirror.commands.goLineEnd(cm);
// make a break for a new line
CodeMirror.commands.newlineAndIndent(cm);
// filled a content of the new line content from line above it
cm.doc.replaceSelection(line_content);
// restore position cursor on the new line
cm.doc.setCursor(current_cursor.line + 1, current_cursor.ch);
};
Run Code Online (Sandbox Code Playgroud)
第2步.
重启Jupyter
结果
在下一个环境中测试
wlysenko@wlysenko-Aspire ~ $ google-chrome --version
Google Chrome 53.0.2785.116
wlysenko@wlysenko-Aspire ~ $ jupyter --version
4.1.0
wlysenko@wlysenko-Aspire ~ $ uname -a
Linux wlysenko-Aspire 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)
小智 8
这是对这个伟大答案的简单调整,满足了 dasWesen 避免双标签的要求。此版本使用 CodeMirror 的 goLineStartSmart 函数仅转到当前行文本的开头,以便在复制文本时不会抓取前导空格或制表符。
在 Seti 的帖子中提到,将代码放在文件~/.jupyter/custom/custom.js 中
在 Windows 上,我在 中找到了 .jupyter 文件夹C:\Users\YourUserName,然后必须创建\custom文件夹和custom.js文件。重新启动 Jupyter 即可获得更改。
CodeMirror.keyMap.pcDefault["Ctrl-Down"] = function(cm){
// get current cursor position
var current_cursor = cm.doc.getCursor();
// First go to end of line, to avoid the problem where if cursor was at start
// of indented text, goLineStartSmart would go to very beginning of line,
// and so we'd get unwanted tabs/spaces in the getRange function.
CodeMirror.commands.goLineEnd(cm);
// now we can safely call goLineStartSmart
CodeMirror.commands.goLineStartSmart(cm);
var start_cursor = cm.doc.getCursor();
var start = {'line': start_cursor.line, 'ch': start_cursor.ch};
// go to the end of line
CodeMirror.commands.goLineEnd(cm);
var end_cursor = cm.doc.getCursor();
var end = {'line': end_cursor.line, 'ch': end_cursor.ch};
// get content
var line_content = cm.doc.getRange(start, end);
// make a break for a new line
CodeMirror.commands.newlineAndIndent(cm);
// filled a content of the new line content from line above it
cm.doc.replaceSelection(line_content);
// restore position cursor on the new line
cm.doc.setCursor(current_cursor.line + 1, current_cursor.ch);
};
Run Code Online (Sandbox Code Playgroud)