Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use Vim keybindings in Google Colaboratory?

Google colaboratory is an amazing tool and the only thing that keeps me from using it more often is the absence of vim keybindings. Is there a way to enable it just like its possible to do for jupyter notebooks and jupyter lab?

like image 241
Mikhail Sirotenko Avatar asked Feb 07 '18 22:02

Mikhail Sirotenko


People also ask

How do you get to the next cell in Colab?

My solution: in "keyboard preferences" I set ^ + j to "Next cell" and ^ + k to "Previous cell". You can also just use the default key bindings but I personally found it easier this way.


2 Answers

It's now available in google colab by default. You can select Vim keybindings from Tools > Settings > Editor, or by typing Ctrl+M H

like image 137
Thibaut Vercueil Avatar answered Sep 30 '22 14:09

Thibaut Vercueil


Here is a little hack to enable Vim mode via the JavaScript console. Annoyingly, it needs to be re-run each time a cell is executed, so I've bound ctrl/cmd-enter and shift-enter to do just that:

function enable_vim() { document.querySelectorAll(".CodeMirror").forEach(function (e) { e.CodeMirror.setOption("vimMode", true); }); }
document.addEventListener('keydown', function(e) {
  if (e.keyCode == 13 && e.metaKey || e.keyCode == 13 && e.shiftKey) {
    for (var i = 0; i < 10; i++) setTimeout(enable_vim, 1000 * i);
  }
});
enable_vim();

Just be careful to be in normal mode (not in input mode) when running a cell. Otherwise Vim mode will be permanently disabled for that cell and one needs to reload the entire web app to re-enable it.

like image 27
Robin Dinse Avatar answered Sep 30 '22 16:09

Robin Dinse