Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save llinfeng/f50211be8ff2c8816934da266403f47d to your computer and use it in GitHub Desktop.
Save llinfeng/f50211be8ff2c8816934da266403f47d to your computer and use it in GitHub Desktop.
Custom vim keybinds for Overleaf (compatible with CodeMirror 5/6)
// ==UserScript==
// @name Overleaf Editor Custom VIM Keybindings (2023)
// @namespace http://tampermonkey.net/
// @version 0.0.2
// @match https://www.overleaf.com/project/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.addEventListener("UNSTABLE_editor:extensions", (event) => {
const { CodeMirror, CodeMirrorVim, extensions } = event.detail;
// Restore Ctrl+C to copy into system clipboard
CodeMirrorVim.Vim.defineAction('CopySelection', copy_in_visual_mode);
CodeMirrorVim.Vim.mapCommand("<c-c>", 'action', 'CopySelection');
// Mappings in insert mode
CodeMirrorVim.Vim.map("<c-]>", "<Esc>", "insert");
// Remap in normal mode
CodeMirrorVim.Vim.map("$", "g$", "normal");
CodeMirrorVim.Vim.map("0", "g0", "normal");
// User-defined commands
CodeMirrorVim.Vim.defineEx("forward", undefined, buctton_click_forward_search); // ref: https://discuss.codemirror.net/t/vim-how-to-use-defineex/738/2
CodeMirrorVim.Vim.defineEx("pdf", undefined, buctton_click_toggle_pdf_panel); // ref: https://discuss.codemirror.net/t/vim-how-to-use-defineex/738/2
CodeMirrorVim.Vim.defineEx("toc", undefined, buctton_click_toggle_TOC_left_panel); // ref: https://discuss.codemirror.net/t/vim-how-to-use-defineex/738/2
CodeMirrorVim.Vim.defineEx("only", undefined, buctton_click_toggle_editor_only); // ref: https://discuss.codemirror.net/t/vim-how-to-use-defineex/738/2
// Access the functions by defining them as actions first
CodeMirrorVim.Vim.defineAction('jumpToPdf', buctton_click_forward_search);
CodeMirrorVim.Vim.defineAction('WritefullPrevious', writefull_previous_error);
CodeMirrorVim.Vim.defineAction('WritefullNext', writefull_next_error);
// TODO: add maps that woCodeMirror.Vim.unmap(';');
// CodeMirror.Vim.unmap(';'); // Cannot put this here - if uncommented, [s and ]s won't work
CodeMirrorVim.Vim.mapCommand("[s", 'action', 'WritefullPrevious');
CodeMirrorVim.Vim.mapCommand("]s", 'action', 'WritefullNext');
CodeMirrorVim.Vim.mapCommand(";lv", 'action', 'forwardsearch');
CodeMirrorVim.Vim.mapCommand(",lv", 'action', 'forwardsearch');
//CodeMirrorVim.Vim.defineAction(";lv", vim_custom_ct);
//
CodeMirrorVim.Vim.mapCommand("\\lv", "action", "jumpToPdf");
// Work in progress
CodeMirrorVim.Vim.unmap(';');
CodeMirrorVim.Vim.map(";lv", "\\lv", "normal")
});
})();
// Collection of functions
// Clipboard access on Windows machines
function copy_in_visual_mode(cm) {
document.execCommand('copy');
alert("Triggered the copy function");
}
// Navigation releated functions
function buctton_click_forward_search(cm) {
document.querySelector('.fa-arrow-right').click()
};
function buctton_click_toggle_pdf_panel(cm) {
document.querySelector('a[class*="custom-toggler-east"]').click()
};
function buctton_click_toggle_TOC_left_panel(cm) {
document.querySelector('a[tooltip*="the file-tree"]').click()
};
function buctton_click_toggle_editor_only(cm) {
// This is just the sum of the two: toggle toc and toggle pdf
document.querySelector('a[class*="custom-toggler-east"]').click()
document.querySelector('a[tooltip*="the file-tree"]').click()
}
// Spell-checker specific functions
function writefull_next_error(cm) {
alert("Pending implementation for WriteFull Next");
}
function writefull_previous_error(cm) {
alert("Pending implementation for WriteFull Previous");
}
function vim_custom_ct(cm) {
alert("test");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment