1

I'm using a plugin for vim called NERDCommenter which allows me to toggle line of code's commented state. The thing is I want to change the shortcut to ctrl + / or command + /. Online (from other posts I read) it says to not use <C-/>, but to instead use <C-_>. This does not work and only maps the toggle comment command to ctrl underscore.

Here is the code I'm using to do this:

nnoremap <C-_> :call NERDComment(0, "toggle")<CR>

Can someone please let me know what I'm doing wrong? Thank you.

Zemelware
  • 93
  • 8
  • Are you using gvim by any chance? In console vim and nvim-qt, control-/ is recognized as , but gvim seems to not know what to do, and just registers it as a slash with no modifier. – hobbs Sep 26 '20 at 04:34
  • 1
    Does this answer your question? [How to map to toggle comments in vim?](https://stackoverflow.com/questions/9051837/how-to-map-c-to-toggle-comments-in-vim) – Jake Grossman Sep 26 '20 at 05:17

1 Answers1

1

As of writing this, in MacOS using the current/built-in Terminal app (without the use of other software), unfortunately it is not possible to map a Vim shortcut using Control+/ or Command ⌘+/.

This is because neither of these key sequences have control code encodings (more on that here and here).


Option #1 (recommended)

Map Option/Alt+/ to toggle NERDCommenter → mac users must use the real character (÷) generated by Option/Alt sequence to do this:

nmap ÷ <plug>NERDCommenterToggle
vmap ÷ <plug>NERDCommenterToggle<CR>gv

Only works if Terminal > Preferences > Profiles [settings] > Keyboard > 'Use Option as Meta Key' is toggled OFF. More info here.

Option #2 - MacVim

You can use MacVim which allows you to map the Command ⌘ key with <D->. More info here.

Option #3 - iTerm2

Use iTerm2 for your terminal app. This way you can map Control+/ to <C-_> and it will work. For Command ⌘+/ to work, you'll have to create a new keyboard shortcut to send as an escape sequence to Vim. More info here and here.


I wouldn't advise this, but you could also use a program to remap your Command ⌘ or Control modifier keys every time you open a specific app like Terminal. iTerm2 works for this task too, as well as programs like Karabiner -- for older versions of MacOS. According to this post, the newer Karabiner-Elements requires some tweaking for app-specific remapping.

More Resources: here, here, and here.

yona
  • 364
  • 1
  • 8
  • I've already seen that post and tried that. As I've already said, I tried to use but it maps to ctrl underscore instead of ctrl slash. – Zemelware Sep 26 '20 at 04:47
  • 1
    @yona The correct way to say "your question has already been answered here" is to [flag it as a duplicate](https://stackoverflow.blog/2009/05/20/linking-duplicate-questions/). – Jake Grossman Sep 26 '20 at 05:21
  • @Zemelware just to be clear, you've added `nmap NERDCommenterToggle` to your `.vimrc` and when you try `Ctrl+/` in Normal mode, nothing happens? – yona Sep 26 '20 at 07:32
  • @yona, yes I've typed exactly that in my `.vimrc` and tried the shortcut in Normal mode, but `ctrl+/` doesn't comment/uncomment lines, only `ctrl+_` does (and wouldn't that make sense anyway because then how would you map ctrl + underscore to a command?). Also, I'm on macOS but I don't know if that matters. – Zemelware Sep 26 '20 at 17:14
  • @JakeGrossman this is not a duplicate. I explicitly stated in the title that `` doesn't work for me. – Zemelware Sep 26 '20 at 17:16
  • @Zemelware I didn’t mean to imply that it was. “Was your question answered here” is not a good answer, was my point. – Jake Grossman Sep 26 '20 at 17:18
  • 1
    @Zemelware it very much _does matter_ that you're using MacOS -> see https://apple.stackexchange.com/questions/24261/how-do-i-send-c-that-is-control-slash-to-the-terminal. Are you using the builtin Terminal app? – yona Sep 26 '20 at 19:00
  • @yona yes I am using the built-in terminal app. That seems to be the issue as I tried it with iTerm2 and it worked. Is there any possible way to do this with the built-in terminal app? – Zemelware Sep 26 '20 at 19:22
  • @Zemelware I have just updated my answer--was this what you were hoping for? – yona Sep 27 '20 at 06:43
  • @yona thank you. It sucks that I can't do this with the default terminal app, but I think I'm going to switch to iTerm2. Thanks for your help! – Zemelware Sep 28 '20 at 02:02