Overleaf VIM Keybindings and Zen mode

The name

目前為 2025-11-19 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Overleaf VIM Keybindings and Zen mode
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  The name
// @match        https://www.overleaf.com/project/*
// @grant        none
// @license      MIT
// ==/UserScript==


(function() {
    'use strict';

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


    // add custom keybindings
    CodeMirrorVim.Vim.map("j", "gj", "normal");
    CodeMirrorVim.Vim.map("k", "gk", "normal");

    CodeMirrorVim.Vim.map("j", "gj", "visual");
    CodeMirrorVim.Vim.map("k", "gk", "visual");

    // Zen mode
    CodeMirrorVim.Vim.defineAction('toggleinterface', toggle_interface);
    CodeMirrorVim.Vim.mapCommand("<C-f>", "action", "toggleinterface");
});
})();



//--------------------------------------------------
//                Functions
//--------------------------------------------------
let flex_elements, block_elements, pdf_viewer;
let interface_active = true;

const style_pdf_margin = document.createElement('style');
document.head.appendChild(style_pdf_margin);

function toggle_interface() {
    flex_elements = [
        ...document.getElementsByClassName("toolbar"),                              // Top bar and PDF bar
        document.getElementsByClassName("cm-panels")[0],                            // Tex bar
        document.getElementsByClassName("review-mode-switcher-container")[0]        // Bottom for changing edit/review mode
    ];

    block_elements = [
        ...document.getElementsByClassName("horizontal-resize-handle")              // Vertical bars
    ];
    pdf_viewer = document.querySelector('[data-testid="pdf-viewer"]');              // PDF viewer, for removing margins


    interface_active = ! interface_active;

    if (interface_active) {
        // Reset orginal setting
        flex_elements.forEach(e => e.style.display = "flex");
        block_elements.forEach(e => e.style.display = "block");
        pdf_viewer.style.top = "var(--toolbar-small-height)";

        style_pdf_margin.textContent = `
            .page {
                margin: var(--spacing-05) auto !important;
            }
        `;

    } else {
        // Remove innecessary ui
        flex_elements.forEach(e => e.style.display = "none");
        block_elements.forEach(e => e.style.display = "none");
        pdf_viewer.style.top = 0;

        style_pdf_margin.textContent = `
            .page {
                margin: 0 !important;
            }
        `;
    }
}