0

I am using bash on WSL2 with Ubuntu, and I want Crtl-i and TAB to send different keycodes so I can remap Ctrl-i on emacs while still using TAB for indentation. I tried following the answer in this question, which suggests remapping Ctrl-i by editing the .Xresources file and then running xrdb ~/.Xresources. I have this in my .Xresources file

XTerm*metaSendsEscape: true
XTerm*eightBitInput: false

Xterm*Translations: #override
Ctrl ~Meta ~Shift <Key>i :string("\033[105;5u")

Except when calling xrdb .Xresources it just gives me this message

xrdb: Connection refused
xrdb: Can't open display 'localhost:0.0'

which seems to be due the fact that WSL is a text only editor, and my changes to the file haven't had an effect. I ran echo $TERM on bash to make sure it is using xterm and it returned xterm-256color, so that seems to be right.

Is there a way to get WSL to use .Xresources or any other way to differentiate Ctrl-i from TAB in WSL?

1 Answers1

0

There's some good research there on your part, coupled with a bit of a misunderstanding of the terminal situation in WSL.

The question/answer you linked to specifically gave those xrdb instructions for xterm. There are many terminals that might be used, and the method for remapping differs for each. That particular answer even gave solutions for three different terminals:

  • Xterm
  • Urxvt
  • Kitty

And while it's possible to use those under WSL (see the end of this answer), none of those are the default WSL terminal. There are two possible default terminals for WSL:

  • In Windows 10, the legacy Windows Console Host. This is the same terminal that is used for CMD and PowerShell.
  • In Windows 11, the (fairly) new Windows Terminal replaces Console Host. It's quite advanced compared to its predecessor, including many additional features. Terminal can also be installed in Windows 10, but it can't be set as the default there.

So you need instructions for the terminal you are using, not really for WSL.

Windows Console Host, to my knowledge, doesn't include any key remapping features. However, you could use something like AutoHotkey or (possibly) AutoIt to intercept the Ctrl+i and send the correct escape sequence. It appears to me that at least AutoHotkey has this ability, but I haven't tried it for this purpose.

However, I would recommend going ahead and installing Windows Terminal (if you haven't already). It's available in the Microsoft Store for Windows 10 (and automatically installed in recent Windows 11 versions).

It does include the ability to remap keys, but it's a little "hidden" behind the configuration file. While Terminal now includes a GUI for editing some settings, advanced features like this do require manually editing of the settings.json.

To do so:

  • In Windows Terminal, go to the dropdown menu and open Settings

  • At the bottom left corner of the Settings, select the "Open JSON file" option.

  • Side-note: It's recommended that you have VSCode installed, or at least some editor that can handle JSON well. VSCode has the added advantage of understanding the Windows Terminal schema, so it will auto-complete the options as you type.

  • Add the following in the "actions" list:

    {
        "command": {
            "action": "sendInput",
            "input": "\u001b[105;5u"
        },
        "keys": "ctrl+i"
    }
    

While I haven't tested this with Emacs, I did confirm in the Fish shell's fish_key_reader that Ctrl+i is sending the specified control codes rather than a Tab character.

Using Linux/X terminals in WSL

If you do want to use one of the Linux terminals, you can do so with WSL. You'll just need to either be using Windows 11 with WSLg support (which allows you to run Linux GUI applications in WSL2) or configure GUI support in Windows 10 using one of the methods I detail in this Super User answer. With that in place, you could, if you want to, run Xterm and use the instructions from the answer you linked.

NotTheDr01ds
  • 3,547