WSL Terminal – How to Use Shift-Insert to Paste

terminalwindows-subsystem-for-linux

# When I do just Shift-Insert, I get
~$ 2~
# When I do Ctrl-V, then Shift-Insert, I get
~$ ^[[2;2~

Shift-insert works well in other situations, like Windows CMD or Git-Bash

In wsl, I can use Ctrl-Shift-V to paste, but prefer shift-insert.

Is there any work-around ?

Best Answer

According to microsoft/WSL:

Note that WSL distro's launch in the Windows Console (unless you have taken steps to launch a 3rd party console/terminal). Therefore, please file UI/UX related issues in the Windows Console issue tracker.

But the given link for Windows Console points to Windows Terminal:

The new Windows Terminal and the original Windows console host, all in the same place!

There's no (usable) documentation, so your question has to be answered by pointing into its source-code.

The relevant chunk (which you'd like to exercise) is here, in windowio.cpp:

    // handle shift-ins paste
    if (inputKeyInfo.IsShiftOnly() && ShouldTakeOverKeyboardShortcuts())
    {
        if (!bKeyDown)
        {
            return;
        }
        else if (VirtualKeyCode == VK_INSERT && !(pSelection->IsInSelectingState() && pSelection->IsKeyboardMarkSelection()))
        {
            Clipboard::Instance().Paste();
            return;
        }
    }

Half of the conditions (to reach that Paste()) appear likely to be met (barring some bug in this program). The ones that aren't apparent:

  • ShouldTakeOverKeyboardShortcuts() — but this is used in the ctrl+shift+plus/minus code

  • pSelection->IsKeyboardMarkSelection() — we're assuming the mouse was used for selection.

But that's assuming that this HandleKeyEvent method is treating the two different key sequences equally. The ^[[2;2~ comes from another part of the program, in terminalInput.cpp, using a built-in table

// Sequences to send when a modifier is pressed with any of these keys
// Basically, the 'm' will be replaced with a character indicating which
//      modifier keys are pressed.
static constexpr std::array<TermKeyMap, 22> s_modifierKeyMapping{

and applied here:

// If a modifier key was pressed, then we need to try and send the modified sequence.
if (keyEvent.IsModifierPressed() && _searchWithModifier(keyEvent, senderFunc))
{
    return true;
}

From reading the code, it appears that this is all upstream from the windowio.cpp logic, so that combination will never be reached. The developers provided no way to override or modify this behavior.

As suggested in a comment by @Rody-Oldenhuis:

You can use wsltty; this supports Ctrl+Ins/Shift-Ins out of the box

(which is derived from mintty).

Related Question