jupyter中使用熟悉的vim


原文地址: https://blog.csdn.net/qq_43827595/article/details/104454436

 配置好了python的開發環境jupyter,的確很順手,但是有點讓人不爽的就是還是需要不時的動下鼠標,讓用了這么就的vimer的我非常不爽,下面開始改造開始:

一、項目地址:lambdalisue/jupyter-vim-binding,該項目能夠讓vimer在jupyter notebook開啟vim模式,大大提升我們的工作效率,二話不說,當然是安裝了

  一般人的$(jupyter --data-dir)都是/home/nication/.local/share/jupyter,如果不確定,就在終端下運行:echo $(jupyter --data-dir),如果不想倒騰,就直接用以下的命令吧:

  mkdir -p $(jupyter --data-dir)/nbextensions                //新建插件目錄

  cd $(jupyter --data-dir)/nbextensions                  //切換到插件的目錄

  git clone https://github.com/lambdalisue/jupyter-vim-binding vim_binding  //下載插件

  jupyter nbextension enable vim_binding/vim_binding          //啟用插件

二、修改配置文件:

  cd  ~/.jupyter                            //切換到配置文件的目錄

  mkdir  custom/                            //新建配置文件夾

  cd custom                              //切換至配置文件夾

  touch custom.js                            //新建配置文件

// Configure CodeMirror Keymap
require([
  'nbextensions/vim_binding/vim_binding',   // depends your installation
], function() {
  // Map jj to <Esc> 這里我加了個l,是為了保證光標在同一個位置
  //CodeMirror.Vim.map("jj", "<Esc>l", "insert");

  // 我自定義的快捷鍵
  CodeMirror.Vim.map("H", "^", "normal");
  CodeMirror.Vim.map("L", "$", "normal");
  CodeMirror.Vim.map("dH", "d0", "normal");
  CodeMirror.Vim.map("s", ":q", "normal");    

  // Swap j/k and gj/gk (Note that <Plug> mappings)
  CodeMirror.Vim.map("j", "<Plug>(vim-binding-gj)", "normal");
  CodeMirror.Vim.map("k", "<Plug>(vim-binding-gk)", "normal");
  CodeMirror.Vim.map("gj", "<Plug>(vim-binding-j)", "normal");
  CodeMirror.Vim.map("gk", "<Plug>(vim-binding-k)", "normal");
});

// Configure Jupyter Keymap
require([
  'nbextensions/vim_binding/vim_binding',
  'base/js/namespace',
], function(vim_binding, ns) {
  // Add post callback
  vim_binding.on_ready_callbacks.push(function(){
    var km = ns.keyboard_manager;
    // Allow Ctrl-2 to change the cell mode into Markdown in Vim normal mode
    km.edit_shortcuts.add_shortcut('ctrl-2', 'vim-binding:change-cell-to-markdown', true);
    // Update Help
    km.edit_shortcuts.events.trigger('rebuild.QuickHelp');
  });
});

// Selecting all
require([
  'nbextensions/vim_binding/vim_binding',
], function() {
   CodeMirror.Vim.map("<C-a>", "ggVG", "normal");
});

// Use Ctrl + s to save in Vim normal and insert mode
require([
  'nbextensions/vim_binding/vim_binding',
  'base/js/namespace',
], function(vim_binding, ns) {
  // Add post callback
  vim_binding.on_ready_callbacks.push(function(){
    var km = ns.keyboard_manager;
    // Indicate the key combination to run the commands
    km.edit_shortcuts.add_shortcut('ctrl-s', 'jupyter-notebook:save-notebook', true);
    // Update Help
    km.edit_shortcuts.events.trigger('rebuild.QuickHelp');
  });
});

  將上邊的內容添加到custom.js即可。

三、重新啟動yupyter notebook即可,在代碼編輯區輸入代碼,退出插入模式后,依舊可以shift+return輸出運行結果,再也不用鼠標,完美收工。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM