Help

How can I define custom Vim macros in a vimrc file on Overleaf?

Overleaf doesn't yet have built-in support for vimrc files for its Vim mode, but in the meantime you can add vimrc functionality using a browser extension by following the steps below.

Note: this method is not officially supported. Installing custom scripts can cause many problems. If you experience problems, please try disabling any custom scripts before contacting support.

Overleaf's Vim mode is provided by the open source CodeMirror editor, so you can consult the CodeMirror manual and the source code to discover more ways of customising the Vim mode. The approach here is based on this CodeMirror GitHub issue.

Adding Custom Vim Macros in FireFox with Greasemonkey

Greasemonkey is a popular extension for FireFox that lets you run custom scripts whenever you load a page. We can use it to run a short custom script to customise the Vim mode.

  1. Install the GreaseMonkey add-on in your browser.
  2. Create a New User Script.
  3. Choose a Name, e.g. My Overleaf Vim Macros.
  4. Choose a Namespace. In this example, I've used overleaf in the screenshot below, but you should use your own namespace, e.g. your email address, which is what Greasemonkey suggests.
  5. Set the Includes to be https://www.overleaf.com/*
  6. Create the script.
  7. Enter the following JavaScript for the script:
    $(function () {
      // Make sure CodeMirror is loaded.
      if (!window.CodeMirror || !CodeMirror.Vim) return
    
      // Install custom macros.
      CodeMirror.Vim.map('jj', '<Esc>', 'insert')
    })
    
    I've included one popular map as an example: it lets you type jj in insert mode to escape back to normal mode. You can add other maps by adding more calls to CodeMirror.Vim.map where indicated.
  8. Save the script.
  9. Reload any open Overleaf editors. Your macros should now be available in Overleaf.

The script should look something like:

Greasemonkey script for vimrc on Overleaf

Adding Custom Vim Macros in Chrome

In Chrome (and several other browsers), you can use Tampermonkey to run the script above to set up your custom Vim macros on Overleaf.