Ctrl underscore sends RET in Emacs and terminal #15164

Closed
opened 2026-01-31 04:30:21 +00:00 by claunia · 14 comments
Owner

Originally created by @sivaramn on GitHub (Sep 10, 2021).

Windows Terminal version (or Windows build number)

Windows Terminal Version: 1.9.1942.0

Other Software

Bug is seen in Emacs 27.2 too in WSL

 wsl --list -v
 NAME            STATE           VERSION
 * Ubuntu-20.04    Running         2

Steps to reproduce

ctrl underscore got by pressing ctrl and _ (underscore)key sends RET in terminal as well as emacs in WSL. ubuntu 20.04

 $uname -a
 Linux CompName 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021 x86_64 x86_64 x86_64     GNU/Linux

$ showkey -a

Press any keys - Ctrl-D will terminate this program

_        95 0137 0x5f
^M       13 0015 0x0d
^D        4 0004 0x04

Expected Behavior

In Emacs Ctrl + _ is usually mapped to undo. It seems to be mapped to RET in emacs as well as terminal as reported above

Actual Behavior

getting RET passed to terminal and in emacs instead of _ or undo in emacs

Originally created by @sivaramn on GitHub (Sep 10, 2021). ### Windows Terminal version (or Windows build number) Windows Terminal Version: 1.9.1942.0 ### Other Software Bug is seen in `Emacs 27.2` too in WSL wsl --list -v NAME STATE VERSION * Ubuntu-20.04 Running 2 ### Steps to reproduce ctrl underscore got by pressing ctrl and _ (underscore)key sends RET in terminal as well as emacs in WSL. ubuntu 20.04 $uname -a Linux CompName 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux $ showkey -a Press any keys - Ctrl-D will terminate this program _ 95 0137 0x5f ^M 13 0015 0x0d ^D 4 0004 0x04 ### Expected Behavior In Emacs Ctrl + _ is usually mapped to `undo`. It seems to be mapped to `RET` in emacs as well as terminal as reported above ### Actual Behavior getting RET passed to terminal and in emacs instead of _ or `undo` in `emacs`
Author
Owner

@DHowett commented on GitHub (Sep 10, 2021):

What keyboard layout are you using? This is what I am seeing with US-104:

image

@DHowett commented on GitHub (Sep 10, 2021): What keyboard layout are you using? This is what I am seeing with US-104: ![image](https://user-images.githubusercontent.com/189190/132889100-8d9c5262-2fcb-42c3-ace7-307c77fe369a.png)
Author
Owner

@sivaramn commented on GitHub (Sep 10, 2021):

image

does this help? And I don't remember changing my keyboard settings for quite some time; this seems to be recent. Unless, the windows update cycle has changed it, the keyboard layout that is.

@sivaramn commented on GitHub (Sep 10, 2021): ![image](https://user-images.githubusercontent.com/1205075/132889757-0ff0e084-ff9f-44ee-8878-56934a7c04f3.png) does this help? And I don't remember changing my keyboard settings for quite some time; this seems to be recent. Unless, the windows update cycle has changed it, the keyboard layout that is.
Author
Owner

@elsaco commented on GitHub (Sep 11, 2021):

@sivaramn it depends how the keys are pressed. For Emacs undo to work first press crtl then shift+underscore. This will execute undo function in emacs inside WSL instance.

Here's a sample screenshot, notice Undo! in the echo area:

wsl_emacs_undo

And this is the output of showkey:

_        95 0137 0x5f
^_       31 0037 0x1f

First line is ctrl followed by shift+underscore. The 2nd line is crtl+shift+underscore.

@elsaco commented on GitHub (Sep 11, 2021): @sivaramn it depends how the keys are pressed. For Emacs undo to work first press crtl then shift+underscore. This will execute undo function in emacs inside WSL instance. Here's a sample screenshot, notice Undo! in the echo area: ![wsl_emacs_undo](https://user-images.githubusercontent.com/3933920/132932244-d98fe6b0-c3be-4190-bb7b-11c9056131cd.png) And this is the output of showkey: ``` _ 95 0137 0x5f ^_ 31 0037 0x1f ``` First line is ctrl followed by shift+underscore. The 2nd line is crtl+shift+underscore.
Author
Owner

@sivaramn commented on GitHub (Sep 11, 2021):

@sivaramn it depends how the keys are pressed. For Emacs undo to work first press crtl then shift+underscore. This will execute undo function in emacs inside WSL instance.
First line is ctrl followed by shift+underscore. The 2nd line is crtl+shift+underscore.

Yes, I'm aware of this. I've been using emacs for quite some time and know to C-S _ . C-h k calls RET from simple.el for the key chord which was weird. This seems to be in WSL terminal only as other terminals dont have this issue.

@sivaramn commented on GitHub (Sep 11, 2021): > @sivaramn it depends how the keys are pressed. For Emacs undo to work first press crtl then shift+underscore. This will execute undo function in emacs inside WSL instance. > First line is ctrl followed by shift+underscore. The 2nd line is crtl+shift+underscore. Yes, I'm aware of this. I've been using emacs for quite some time and know to C-S _ . `C-h k` calls `RET` from `simple.el` for the key chord which was weird. This seems to be in WSL terminal only as other terminals dont have this issue.
Author
Owner

@sivaramn commented on GitHub (Sep 11, 2021):

problem seems to go away if I switch to
image

user1@CompName:~$ showkey -a

Press any keys - Ctrl-D will terminate this program

^_       31 0037 0x1f
^D        4 0004 0x04

Still, wouldn't it be a bug that it works differently when the keyboard is switched?

@sivaramn commented on GitHub (Sep 11, 2021): problem seems to go away if I switch to ![image](https://user-images.githubusercontent.com/1205075/132938382-25d1e8ae-cf5f-47fb-99f0-3c121d737aae.png) user1@CompName:~$ showkey -a Press any keys - Ctrl-D will terminate this program ^_ 31 0037 0x1f ^D 4 0004 0x04 Still, wouldn't it be a bug that it works differently when the keyboard is switched?
Author
Owner

@elsaco commented on GitHub (Sep 11, 2021):

@sivaramn Emacs has "C+x u" for undo actions. Why not use this key combination instead of "C+S+_" which I find more difficult to type than "C+x u".

@elsaco commented on GitHub (Sep 11, 2021): @sivaramn Emacs has "C+x u" for undo actions. Why not use this key combination instead of "C+S+_" which I find more difficult to type than "C+x u".
Author
Owner

@sivaramn commented on GitHub (Sep 12, 2021):

@sivaramn Emacs has "C+x u" for undo actions. Why not use this key combination instead of "C+S+_" which I find more difficult to type than "C+x u".

True. But I work with both Win and Linux OSes, ctrl-X does cut in Win apps; Ctrl _ works universally without any side effects and unintended results.

@sivaramn commented on GitHub (Sep 12, 2021): > > > @sivaramn Emacs has "C+x u" for undo actions. Why not use this key combination instead of "C+S+_" which I find more difficult to type than "C+x u". True. But I work with both Win and Linux OSes, ctrl-X does cut in Win apps; Ctrl _ works universally without any side effects and unintended results.
Author
Owner

@zadjii-msft commented on GitHub (Sep 13, 2021):

Still, wouldn't it be a bug that it works differently when the keyboard is switched?

Yep, that's a real bug alright. There's a little bit of weird handling for a couple ctrl+key combos, ^_ is one of them. I'll toss this on the backlog, thanks for following up!

@zadjii-msft commented on GitHub (Sep 13, 2021): > Still, wouldn't it be a bug that it works differently when the keyboard is switched? Yep, that's a real bug alright. There's a little bit of weird handling for a couple ctrl+key combos, ^_ is one of them. I'll toss this on the backlog, thanks for following up!
Author
Owner

@Mathiasdm commented on GitHub (Jan 20, 2022):

In case it helps: I get this as well in nano and in the terminal, keyboard is 'Belgian (Period)'.

@Mathiasdm commented on GitHub (Jan 20, 2022): In case it helps: I get this as well in nano and in the terminal, keyboard is 'Belgian (Period)'.
Author
Owner

@hvesalai commented on GitHub (Nov 16, 2023):

I get this for Ctrl+Shift+Minus (i.e. CTRL+_)

$ showkey -a

Press any keys - Ctrl-D will terminate this program

^M       13 0015 0x0d

I would also like to see a very quick fix to this issue as I use Ctrl+_ as my Undo in emacs.

I installed my windows from scratch today and don't remember seeing this before, so something changed when I installed everything again.

@hvesalai commented on GitHub (Nov 16, 2023): I get this for `Ctrl+Shift+Minus` (i.e. `CTRL+_`) ``` $ showkey -a Press any keys - Ctrl-D will terminate this program ^M 13 0015 0x0d ``` I would also like to see a very quick fix to this issue as I use `Ctrl+_` as my Undo in emacs. I installed my windows from scratch today and don't remember seeing this before, so something changed when I installed everything again.
Author
Owner

@j4james commented on GitHub (Nov 25, 2023):

For those of you that are getting a ^M when your press Ctrl+Shift+Minus, this is because your keyboard driver doesn't have a mapping for that key combination, so the terminal is forced to generate an appropriate mapping itself. The way it does that is by taking the base key (in this case -, which is ASCII 0x2D), and masking off the lower 5 bits. The result of that mask is 0x0D, which is ^M.

The reason it works correctly on a US keyboard (and several others) is because the keyboard itself has a built in mapping for that key combination, so it doesn't rely on the terminal to do the translation.

I've recently been experimenting with a rewrite of the VT keyboard handler which fixes this bug (amongst other things). However, I don't want to promise that it's going to be fixed soon - it's still very much a work in progress.

@j4james commented on GitHub (Nov 25, 2023): For those of you that are getting a `^M` when your press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Minus</kbd>, this is because your keyboard driver doesn't have a mapping for that key combination, so the terminal is forced to generate an appropriate mapping itself. The way it does that is by taking the base key (in this case `-`, which is ASCII `0x2D`), and masking off the lower 5 bits. The result of that mask is `0x0D`, which is `^M`. The reason it works correctly on a US keyboard (and several others) is because the keyboard itself has a built in mapping for that key combination, so it doesn't rely on the terminal to do the translation. I've recently been experimenting with a rewrite of the VT keyboard handler which fixes this bug (amongst other things). However, I don't want to promise that it's going to be fixed soon - it's still very much a work in progress.
Author
Owner

@hvesalai commented on GitHub (Nov 25, 2023):

@j4james do you mean that the US keyboard has a mapping for Shift+Minus or specifically for Ctrl+Shift+Minus.

Indeed I'm on a Finnish keyboard, which is quite different from the US one. On my keyboard Shift+Minus is mapped to produce the underscore, i.e. _, which is 0x5F.

@hvesalai commented on GitHub (Nov 25, 2023): @j4james do you mean that the US keyboard has a mapping for `Shift`+`Minus` or specifically for `Ctrl`+`Shift`+`Minus`. Indeed I'm on a Finnish keyboard, which is quite different from the US one. On my keyboard `Shift`+`Minus` is mapped to produce the underscore, i.e. `_`, which is `0x5F`.
Author
Owner

@j4james commented on GitHub (Nov 25, 2023):

@hvesalai I mean it has a mapping specifically for Ctrl+Shift+Minus. You can see what the mappings are on the Keyboard Layout Info website. Compare the US keyboard layout and the Finnish layout.

On both of them, the minus key is VK_OEM_MINUS (although the position of the key is different). And the US layout has the SHIFT+CONTROL state for that key mapped to (i.e. 0x1F), while the Finnish keyboard has the CONTROL state alone mapped to . Many keyboard layouts won't have a mapping for that character at all.

What that means is that on your keyboard, Ctrl+Minus should produce the 0x1F character, but don't get too use to that behavior. With my new keyboard implementation that'll stop working, and you'll need to use Ctrl+Shift+Minus, the same as you would on a US keyboard.

@j4james commented on GitHub (Nov 25, 2023): @hvesalai I mean it has a mapping specifically for <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Minus</kbd>. You can see what the mappings are on the [Keyboard Layout Info](https://www.kbdlayout.info/) website. Compare the [US](https://www.kbdlayout.info/kbdus/shiftstates) keyboard layout and the [Finnish](https://www.kbdlayout.info/kbdfi/shiftstates) layout. On both of them, the minus key is `VK_OEM_MINUS` (although the position of the key is different). And the US layout has the `SHIFT`+`CONTROL` state for that key mapped to `␟` (i.e. `0x1F`), while the Finnish keyboard has the `CONTROL` state alone mapped to `␟`. Many keyboard layouts won't have a mapping for that character at all. What that means is that on your keyboard, <kbd>Ctrl</kbd>+<kbd>Minus</kbd> should produce the `0x1F` character, but don't get too use to that behavior. With my new keyboard implementation that'll stop working, and you'll need to use <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Minus</kbd>, the same as you would on a US keyboard.
Author
Owner

@hvesalai commented on GitHub (Nov 25, 2023):

@j4james OK, that makes a lot of sense. Indeed when using the Terminal, CTRL+Minus works as an Undo in emacs (after I remove the mapping from Terminal's zoom/scaling).

@hvesalai commented on GitHub (Nov 25, 2023): @j4james OK, that makes a lot of sense. Indeed when using the Terminal, `CTRL`+`Minus` works as an `Undo` in emacs (after I remove the mapping from Terminal's zoom/scaling).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#15164