How to disable the command key in Vim in Mac?
I accidentally press command + w and close the view or command + p and print opens up, it is super annoying.
I couldn't find any reference on this matter on Google.
Thank you for your help.
If, by 'Vim in Mac', you mean MacVim as opposed to simply running vim in a terminal emulator on Mac, then you're going to run into some problems with vim having certain expectations, and MacOS having certain expectations regarding the menu.
If you don't care about having 'Close Window' and 'Print' in your menu at all, you can add lines like
:aunmenu File.Close\ Window and
:aunmenu File.Print (
:help aunmenu) to your
.gvimrc. If you still want them in the menu, there's a half-solution that involves both vim and MacOS.
MacOS allows you to arbitrarily set keyboard shortcuts to perform menu items in an application. Vim allows you to both create menu items that do anything, and includes nothing as one of those possible actions.
.gvimrc, you'll need to define a menu item for the
<Nop> mapping (
:help <Nop>). I'd probably put it in Tools, with the
:amenu Tools.NOP <Nop>. Doing this, and either restarting MacVim or sourcing the dotfile should show a NOP command in the Tools menu. Click it, it should do nothing.
:amenu ensures that it's present in all modes.
Then, on the MacOS side, you'll need to open System Preferences, go to the Keyboard preference pane, go to the Shortcuts tab, choose App Shortcuts from the list on the left, and then press the plus button on the list on the right to start overwriting the default shortcuts. MacVim may not show up in the Application list, you may need to find it by choosing 'Other...'. For Menu Title, just enter NOP (or whatever you called it — but you only need the name of the entry itself, not what menu it's in). Finally, hit Cmd-W for Keyboard Shortcut.
Now we run into why this is a half-solution — any given menu item can only have one keyboard shortcut. You could, if you wish, create NOP1, NOP2, etc. It starts to feel more and more like a hack, but it should work.