2015-03-24 9 views
10

आईपीथॉन नोटबुक वातावरण में, आईपीथॉन जावास्क्रिप्ट एपीआई का उपयोग कर कस्टम कीबोर्ड शॉर्टकट को परिभाषित करना संभव है। %%javascript जादू का उपयोग करना, एक IPython के इंटरैक्टिव सांत्वना के भीतर एक जावास्क्रिप्ट इस प्रकार लिख सकते हैं (उदाहरण के वर्णित here):संपादन मोड में वर्तमान लाइन को डुप्लिकेट करने के लिए कस्टम आईपीथन नोटबुक कीबोर्ड शॉर्टकट

%%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- बांधता है बनाता है लिखने के लिए करना चाहते हैं 'डुप्लिकेट वर्तमान लाइन' की कार्रवाई के लिए नीचे --- अर्थात, कर्सर को वर्तमान रेखा की शुरुआत में ले जाएं, रेखा का चयन करें, लाइन कॉपी करें, वापसी करें, पेस्ट करें। अनिवार्य रूप से, मैं ग्रहण के कीबोर्ड शॉर्टकट, या नोटपैड ++ में Ctrl-d, या सी-ए-सी-स्पेस सी-एन एम-डब्ल्यू सी-वाई Emacs में अनुकरण करना चाहता हूं। जावास्क्रिप्ट फ़ाइल निम्न का रूप ले जाएगा:

%%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 के लिए किसी भी प्रलेखन नहीं मिल सकता है ।

मैं एक (उदा।) ऑटोहॉटकी समाधान के साथ नहीं जाऊंगा क्योंकि मैं इस शॉर्टकट को आईपीथन नोटबुक के संपादन मोड में प्रतिबंधित करना चाहता हूं।

उत्तर

6

~/.jupyter/कस्टम में जोड़े/अगले कोड custom.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); 
}; 

परिणाम

enter image description here

एक अगली वातावरण में परीक्षण किया गया

[email protected] ~ $ google-chrome --version 
Google Chrome 53.0.2785.116 
[email protected] ~ $ jupyter --version 
4.1.0 
[email protected] ~ $ 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 
संबंधित मुद्दे