Providing custom Vim key bindings

Overleaf provides Vim and Emacs keybindings that can be switched on in the Project menu. However, as these keybindings are emulated by the editor (i.e., there is no actual Vim or Emacs interpreter running), it’s not possible to use Vim’s standard methods of adding new ones within Overleaf using Tampermonkey. Here we provide some methods which should help overcome this limitation.

Caution regarding Tampermonkey and UserScripts

Please be advised that UserScripts and Tampermonkey are not officially supported by Overleaf and that custom scripts can interfere with Overleaf’s normal operations. If you are using such scripts and need to contact Overleaf for technical support, then we kindly ask you to disable any Tampermonkey scripts and check whether the situation improves. If disabling the scripts does not resolve your issue, please contact us for assistance.

Tampermonkey UserScript

Please note that this is not an officially supported method and the interface may change in the future, hence the “UNSTABLE_editor” naming.

// ==UserScript==
// @name         Overleaf Editor Custom VIM Keybindings
// @namespace    http://tampermonkey.net/
// @version      0.1
// @match        https://www.overleaf.com/project/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    window.addEventListener("UNSTABLE_editor:extensions", (event) => {
    const { CodeMirror, CodeMirrorVim, extensions } = event.detail;

    // add custom keybindings - insert mode applies on insert
    CodeMirrorVim.Vim.map("jj", "<Esc>", "insert");
    // normal mode applies while escaped
    CodeMirrorVim.Vim.map("h", "j", "normal");
    });
})();

Last updated

Was this helpful?