macvim-dev / macvim

Vim - the text editor - for macOS
https://macvim.org
Vim License
7.5k stars 681 forks source link

Key combinations involving the Cmd(⌘) key inserts unexpected text. #1254

Closed zzJinux closed 2 years ago

zzJinux commented 2 years ago

Steps to reproduce

  1. Open the MacVim.app and enter INSERT mode
  2. Type some Cmd key combinations that don't map to anything: Cmd+K, Cmd+F8, Cmd+Shift+F8, ...
  3. Each key combination inserts the textual representation of itself:
<D-k>
<D-[>
<D-F8>
<S-D-F7>
<C-S-D-F8>
<M-C-S-D-F8>

I have reproduced the above with following conditions:

Expected behaviour

Except for some predefined keymaps (Copy, Paste, ...), nothing happens.

Version of Vim and architecture

9.0.65 (173), GUI, arm64

Environment

Operating System: Monterey, arm64

How MacVim was installed

GitHub

Logs and stack traces

Nothing is logged

Vim configuration where issue is reproducable

No response

Issue has been tested with given configuration

Issue has been tested with no configuration

Other conditions

adudek16 commented 2 years ago

This isn't a new issue. I can replicate the behavior pre 173. OS: 11.6.8, intel Macbook pro 2019.

ychin commented 2 years ago

That's just how Vim works in general. You also get the same thing with Ctrl modifiers for example. What MacVim does is to make a new modifier <D- to allow mapping of Cmd keys but the core Vim logic is the same.

ychin commented 2 years ago

Actually let met think about this a little more.

eirnym commented 2 years ago

I agree with @ychin. Vim core for each mode has its own mapping table. For keys not listed in the table it print out keys in insert mode.

zzJinux commented 2 years ago

@eirnym @ychin I'm having trouble finding how to disable this behavior. Would you mind if you let me know? Thank you.

eirnym commented 2 years ago

I remember this behavior in vim since forever (not only since I'm using MacVim). I'd suggest to ask in https://github.com/vim/vim or in stack overflow community.

zzJinux commented 2 years ago

@eirnym MacVim.app and vim --clean from Terminal.app shows different result:

INSERT mode mvim --clean vim --clean (Terminal.app) vim --clean (Terminal.app, delete F8-related keymaps)
⌘K Inserts <D-k> Terminal.app clears the screen (same as the left)
⌘Y Inserts <D-y> Nothing happens (bell) (same)
⌘[ Inserts <D-[> Nothing happens (bell) (same)
⌘ F8 Inserts <D-F8> Inserts <F8> Nothing happens (bell)
⇧⌘ F8 Inserts <S-D-F8> Nothing happens (bell) (same as the left)
⌃⇧⌘ F8 Inserts <C-S-D-F8> Nothing happens (bell) (same)
⌥⌃⇧⌘ F8 Inserts <M-C-S-D-F8> Nothing happens (bell) (same)

vim is from the MacVim.app contents.

To me, it is more of MacVim's issue.

eirnym commented 2 years ago

I meant that Vim inserts unmapped sequence to the input directly.

Also Vim interprets keys differently when they come via terminal and GUI. This is common for all GUIs

What exactly is mapped or not it's a different question and I understand that your issue is not about this topic.

zzJinux commented 2 years ago

https://github.com/vim/vim/issues/3036

It implies that there is no way to disable the behavior in question, except that I have to manually map every key combination, which happens to annoy me, to <NOP>. This is so frustrating 😢.

eirnym commented 2 years ago

Additionally, it's well known that Terminal.app consumes all key combinations with Cmd in it( (or cmd itself in some cases). Thus if you want to use these combinations in terminal, I suggest use alternative terminal like Kitty or iTerm. As far as I know they send combinations with cmd to an app

zzJinux commented 2 years ago

@eirnym Thank you! I have confirmed that iTerm.app allows mapping Cmd key combinations while Terminal.app does not; it has very limited options for key combinations (e.g. you can't use alphabet characters).

Back to the main topic, it seems reasonable that this issue be closed 😢.... or there may be other solution?

eirnym commented 2 years ago

You're welcome

ychin commented 2 years ago

So, terminals in general don't know anything about Cmd keys. There aren't standardized ways to send command key combos to Vim or other terminal programs, and Vim itself also doesn't know much about cmd keys. From digging around, Vim's support of the command key (MOD_MASK_CMD) is more than a decade old and seems to be implemented for the old, now defunct, Carbon-based gVim on the Mac (in the now-deleted gui_mac.c), and has special cases sprinkled all over. Since that UI is no longer used, only MacVim uses it.

There is some special code in edit.c's insert_special() function that forcefully prints out sequences like <D-j> when you press Cmd-j. It's also really old code. We could potentially remove it so it doesn't do that, but I also kind of don't feel like doing it, and it's also better done upstream at Vim.

Is there a reason why this bothers you anyway @zzJinux ? Are you making a mistaken Cmd key press and seeing this all the time?

Speaking of which you said iTerm supports the cmd key, is that true @zzJinux ? It seems to have some support but I was struggling to map any <D- keys to Vim and get it to work.

eirnym commented 2 years ago

@ychin yes, iTerm supports passing through keys with Cmd key if they not already bonded with iTerm itself

ychin commented 2 years ago

Closing this issue. This feature has been like this for a while. It's not worth changing it now.

zzJinux commented 2 years ago

@ychin

This turned up to me when, while using Macvim (GUI), one app triggered a werid key combination. <M-C-S-D-F19> is inserted everytime I hit CapsLock key. I have already sent an email to the developer of that app, not receiving any response yet. Subsequently I realized that some other key combinations showed the same behavior.

While using the terminal vim, almost all mistakten key combinations are effectively ignored due to the terminal application's default setting (I use Terminal.app personally). It seems to be gvimrc's responsibility to ignore those while using GUI MacVim.

MacVim may introduce another method in the future. For now, I will go with gvimrc.

ychin commented 2 years ago

Hmm but I mean Terminal would not block it if you hit say <F-10> and it will definitely print that. I’m not sure why it’s blocking the weird combo above but seems like the general issue here is that your tool is generating these key presses.

I kind of prefer not adding MacVim specific changes here and I don’t think think it’s MacVim’s job to block random Function keys just because some tool generates it. I can somewhat see doing something about the <D- Cmd key stuff but seems like that’s not your issue anyway.

Maybe Vim can add an option to not print Function keys (or other unmapped special keys like cmd keys) if they are not mapped (unless in i_Ctrl-V)? I think that’s the best solution I can think of. Feel free to file an issue there.