自定义 IPython 在编辑模式下复制当前行的笔记本键盘快捷方式
Custom IPython Notebook keyboard shortcut to duplicate current line in edit mode
在 IPython Notebook 环境中,可以使用 IPython Javascript API 定义自定义键盘快捷键。使用 %%javascript
魔法,可以在 IPython 的交互式控制台中编写 javascript,如下所示(示例描述 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;
}}
);
我想写一个 javascript 来在编辑模式下创建一个快捷方式,将 Ctrl-Alt-Down 绑定到 'duplicate current line' 的操作---也就是说,将光标移动到当前行的开始,select行,复制行,return,粘贴。本质上,我想模拟 Eclipse 的键盘快捷键,或 Notepad++ 中的 Ctrl-d,或 Emacs 中的 C-a C-SPACE C-n M-w C-y。 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-down' 是表示快捷序列的不正确方式,而且我找不到 keyboard_manager
.
的任何文档
我不想使用(例如)AutoHotKey 解决方案,因为我想将此快捷方式限制为 IPython Notebook 的编辑模式。
第一步.
如果不存在则在~/.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
结果
已在下一个环境中测试
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
这是对 的简单调整,满足了 dasWesen 避免双重制表符的要求。此版本使用 CodeMirror 的 goLineStartSmart 函数转到当前行文本的开头,因此当它复制文本时,它不会获取前导空格或制表符。
如Seti的post所述,将代码放在文件~/.jupyter/custom/custom.js
在 Windows 上,我在 C:\Users\YourUserName
中找到了 .jupyter 文件夹,然后必须创建 \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);
};
PADYMKO 提供了一个很好的答案 。但是,如果您是 mac 用户,则需要稍微修改一下。您需要将代码的第一行更改为:
CodeMirror.keyMap.macDefault["Ctrl-Down"] = function(cm){
其余代码保持不变。这是完整的片段。
CodeMirror.keyMap.macDefault["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);
};
在 IPython Notebook 环境中,可以使用 IPython Javascript API 定义自定义键盘快捷键。使用 %%javascript
魔法,可以在 IPython 的交互式控制台中编写 javascript,如下所示(示例描述 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;
}}
);
我想写一个 javascript 来在编辑模式下创建一个快捷方式,将 Ctrl-Alt-Down 绑定到 'duplicate current line' 的操作---也就是说,将光标移动到当前行的开始,select行,复制行,return,粘贴。本质上,我想模拟 Eclipse 的键盘快捷键,或 Notepad++ 中的 Ctrl-d,或 Emacs 中的 C-a C-SPACE C-n M-w C-y。 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-down' 是表示快捷序列的不正确方式,而且我找不到 keyboard_manager
.
我不想使用(例如)AutoHotKey 解决方案,因为我想将此快捷方式限制为 IPython Notebook 的编辑模式。
第一步.
如果不存在则在~/.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
结果
已在下一个环境中测试
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
这是对
如Seti的post所述,将代码放在文件~/.jupyter/custom/custom.js
在 Windows 上,我在 C:\Users\YourUserName
中找到了 .jupyter 文件夹,然后必须创建 \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);
};
PADYMKO 提供了一个很好的答案
CodeMirror.keyMap.macDefault["Ctrl-Down"] = function(cm){
其余代码保持不变。这是完整的片段。
CodeMirror.keyMap.macDefault["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);
};