自IPython的笔记本电脑键盘快捷键复制在编辑模式下电流线

问题描述 投票:14回答:2

在IPython的笔记本环境中,可以定义使用IPython的Javascript API第自定义快捷键。使用%%javascript魔法,可以如下(实施例描述here)写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;
    }}
);

我想编写创建结合CTRL-ALT-下调至“重复当前行”的行动中编辑模式快捷键一个javascript ---即,将光标移动到当前行的开始,选择行,复制行,退货,粘贴。从本质上讲,我想模仿了Eclipse的键盘快捷键,或在记事本中按Ctrl-d ++或C-A C-SPACE C-N M-W C-Y在Emacs。 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;
    }}
);

虽然我尝试提出“CTRL-ALT-下”是代表快捷顺序不正确的方式,我无法找到一个keyboard_manager任何文件。

与(例如)AutoHotkey的解决方案,因为我想限制此快捷方式IPython的笔记本电脑的编辑模式,我宁可不去。

javascript keyboard-shortcuts ipython-notebook
2个回答
17
投票

第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);
};

第2步。

重新启动Jupyter

结果

enter image description here

经测试,在接下来的环境

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

2
投票

这是一个简单的调整,以塞提Volkylany最伟大的答案,满足dasWesen的要求,以避免增加一倍标签。此版本使用CodeMirror的goLineStartSmart功能可以找到当前行的文字仅仅是开始,这样,当它复制的文本,它不会抢前导空格或制表符。

正如塞提的文章中提到,把代码的文件在〜/ .jupyter /自定义/ 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);
};
© www.soinside.com 2019 - 2024. All rights reserved.