I am a newbie of Vim and I am using MacOS. In MacOS we usually use cmd
and \
to comment the code block and I also want to achieve this in my Vim setting. Following to this post, I know how to modify the ~/.vimrc
to comment different languages like C++ and Python. The remaining part is setting a shortcut key for commenting which I have no clue on that. Is it possible to set cmd
and \
as a shortcut key for commenting in Vim? Or is it possible to use non-alphabetic symbols to form shortcut keys?

- 4,793
- 3
- 12
- 23

- 101
- 3
-
1What did you find so far when you searched for "vim create shortcut"? – mkrieger1 Jul 02 '22 at 11:56
-
@mkrieger1. Thanks for your reply. I do some search on "vim create shortcut". I think what I need to change is the ```noremap``` function to ```
. From ```:help ``` does not work. – Ricky Pang Jul 02 '22 at 12:22 -
`
– romainl Jul 02 '22 at 13:25
1 Answers
First…
In MacOS we usually use
cmd
and\
to comment the code block
No, this has very little to do with macOS. Beyond the basics like "open file", "copy", etc. what sequence or combination of keys does what is decided at the application level. The only OS-specific part, here, is the use of the command
key but macOS editors/IDEs can use whatever they want (IntelliJ uses cmd+/
, for example).
Second…
When Vim is executed in a terminal emulator on macOS, it is incapable of seeing the cmd
key or any combination that includes the cmd
key so you can forget about mapping anything to cmd
+\
in that context.
When using the MacVim GUI, the cmd
key can be used in mappings as <D-
: cmd
+\
would be <D-\>
. While that would work in the MacVim GUI, I would advise against doing it because it is not portable.