[Vim] How to copy selected text in Visual mode with Ctrl+C on Windows?

As a user on Overleaf, I assume I do not have control over which patch is included for CodeMirror. Then, through a Userscript, I wonder if there is a way to let Ctrl+C yank (copy) to the system clipboard in visual mode?

Here is my current attempt that wasn’t doing anything. Actually, Ctrl+C wasn’t remapped at all. I wonder if there is a walk around for assigning shortcuts, for, say, the CodeMirror object itself? The goal is just to yank the visual selection to the system clipboard. I don’t mind if the visual selection should remain there after the system clipboard is updated.

// ==UserScript==
// @name         Overleaf Editor Custom VIM Keybindings (2023)
// @namespace    http://tampermonkey.net/
// @version      0.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;

    // Restore Ctrl+C to copy into system clipboard
    CodeMirrorVim.Vim.defineAction('CopySelection', copy_in_visual_mode);
    CodeMirrorVim.Vim.mapCommand("<c-c>", 'action', 'CopySelection');
    });
})();

// Clipboard access on Windows machines
function copy_in_visual_mode(cm) {
    document.execCommand('copy');
    alert("Triggered the copy function");
}

According to this issue, the + register is made available to Vim mode. Yet, on Overleaf, with :reg, I don’t see it.


Alternatively, it would also be helpful to know if a keyboard shortcut can be assigned to quickly toggle the Vim mode.

In case it also wasn’t obvious to others - although Ctrl+C doesn’t work in Visual mode, it works fine in Insert mode for texts selected either with a mouse, or with shift+arrow keys.

I’d just unmap Ctrl-c, then the browser default of copying to the system clipboard should kick in.

@kometenstaub Do you see what’s off in the example below? It does not do anything when I press Ctrl+C in Visual mode - supposedly, it should yank the selected text to the system clipboard (registry +).

// ==UserScript==
// @name         Test - 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;

    // Attempt to unmap Ctrl+C
    CodeMirrorVim.Vim.unmap('<c-c>');
    CodeMirror.Vim.unmap('<c-c>');
    });
})();

I had a similar issue, switching to “” seemed to work.

Writing it like <C-c> should work.

Great! Good to know that the key-combinations are defined in a case-sensitive manner.

Here is what restores Ctrl+C to copy into the system clipboard, with one caveat:

CodeMirrorVim.Vim.unmap('<C-c>');    
CodeMirrorVim.Vim.map("<C-c>", "<C-c><Esc>");

Caveat: with the second line, after copying the texts into the system clipboard, I need to press j/k to exit visual mode. Otherwise, the visual selection will remain, trapping the cursor from, say, inserting with i or a. This is better than without having defined such <C-c><Esc> mapping, as I would remain in visual model until I press <ESC> again.

Thus, a follow-up question: after hitting <C-c> in visual mode with a chunk of texts selected, how to copy the text to the clipboard and return to normal mode? Mapping <C-c> to <C-c><Esc> does not seem to exist the Visual mode in full.

In the meantime, I added a simple autohotkey mapping that sends Ctrl+C and ESC as physical key presses when I copy with Ctrl+C.