Skip to content

Instantly share code, notes, and snippets.

Last active December 16, 2023 23:39
Show Gist options
  • Save llinfeng/8e003b2107d7ab25e5daedc27c5730ae to your computer and use it in GitHub Desktop.
Save llinfeng/8e003b2107d7ab25e5daedc27c5730ae to your computer and use it in GitHub Desktop.
Greasemonkey Script with Forward search in Overleaf
// ==UserScript==
// @name Overleaf Editor Custom VIM Keybindings
// @namespace
// @version 0.1
// @match*
// @grant none
// Source 1:
// Source 2:
// ==/UserScript==
(function() {
'use strict';
// poll until editor is loaded
const retry = setInterval(() => {
if (window._debug_editors === undefined) return
// get current editor instance
// get current editor instance
var editor = window._debug_editors[window._debug_editors.length -1]
// vim keyboard plugin
var vimKeyboard = window.ace.require("ace/keyboard/vim")
// j k within long lines"j", "gj", "normal")"k", "gk", "normal")
// add custom keybindings - insert mode applies on insert"jj", "<Esc>", "insert")
// Equilivantly, from
// window.ace.require("ace/keyboard/vim")'jj', '<Esc>', 'insert')"jk", "<Esc>", "insert")
// Remapping one key to another is doable, in Normal mode
// `nmap h j` can be defined as the following:
// Though, why should this mapping be here anyways? It is just meant for demo purpose.
//"h", "j", "normal")
// Define local commands ==> matching elements along with a identifier that matches.
// Maintenance note: if one hotkey/cmd does not work, check here for the matching HTML element.
name: "jumpToPdf",
exec: function() {
}, {
name: "VimtexTocToggle",
exec: function() {
document.querySelector('a[tooltip*="the file-tree"]').click()
}, {
name: "CloseComment",
exec: function() {
}, {
name: "TogglePDF",
exec: function() {
document.querySelector('a[tooltip*="the PDF"]').click()
// add keybindings for jump to pdf
vimKeyboard.Vim.mapCommand("\\v", "action", "aceCommand",
{ name: "VimtexTocToggle" }, { context: "normal" });
vimKeyboard.Vim.mapCommand("\\lv", "action", "aceCommand",
{ name: "jumpToPdf" }, { context: "normal" });
vimKeyboard.Vim.mapCommand("\\o", "action", "aceCommand",
{ name: "TogglePDF" }, { context: "normal" });
// bind to ;lv
vimKeyboard.Vim.unmap(",")";lv", "\\lv", "normal")",v", "\\v", "normal")
// Use ,o to activate two hotkeys: hide file-menu and hide PDF preview",o", "\\v\\o", "normal")
vimKeyboard.Vim.defineEx("ShowPDF", "", ()=>editor.commands.exec("TogglePDF"))
vimKeyboard.Vim.defineEx("ClosePDF", "", ()=>editor.commands.exec("TogglePDF"))
vimKeyboard.Vim.defineEx("OpenPDF", "", ()=>editor.commands.exec("TogglePDF"))
vimKeyboard.Vim.defineEx("PDF", "", ()=>editor.commands.exec("TogglePDF"))
vimKeyboard.Vim.defineEx("pdf", "", ()=>editor.commands.exec("TogglePDF"))
// Close comment: CC, cc, CloseComment
vimKeyboard.Vim.defineEx("CloseComment", "", ()=>editor.commands.exec("CloseComment"))
vimKeyboard.Vim.defineEx("CC", "", ()=>editor.commands.exec("CloseComment"))
vimKeyboard.Vim.defineEx("cc", "", ()=>editor.commands.exec("CloseComment"))
// set the modified keyboard handler for editor
console.log("Custom key bindings applied")
}, 100)
Copy link

llinfeng commented Apr 11, 2020

Full Credits to Harutyun Amirjanyan for the jump-to-PDF script, as found on the Ace forum, in this post.

List of mappings defined in this gist.

  • imap jj <esc> and imap jk <esc>;
  • \lv and ;lv are triggering the following button
  • ,v to toggle the file-tree on the left.
  • :pdf in Commandline, to toggle the PDF pane on the right. Alias include: :PDF, :ShowPDF, :ClosePDF, :OpenPDF.
  • cc in Commandline, to toggle the Comment panel. Alias include: :CC, :CloseComment.

It should be plug-and-play, and include settings of the following sort.

  1. nmap and vmap;
  2. unmap certain special keys. (For now, ; and , are specially reserved for unknown purposes.)
  3. command! Short LongCommand, where :Short can be callable. And, in TamperMonkey, the LongCommand needs to be specified through JavaScript.

Copy link

The Violentmonkey add-on for Firefox does not let me to source from this Gist. The script will be updated on here instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment