47

I use vim under iterm2. I'm using the NERDCommenter plugin, and I'd like to use Ctrl+/ to toggle comments (Trying to switch from Idea/Eclipse to vim). This is my mapping in the .vimrc :

 nmap <C-/> <leader>c<Space>
 vmap <C-/> <leader>c<Space>

But it doesn't seem to work. What can be the reason?

dev_nut
  • 2,346
  • 3
  • 27
  • 42

4 Answers4

69

For some reason, vim registers <C-/> as <C-_> (you can see it in insert mode using <C-v><C-/>). It can be the terminal or a historical design thing that terminal apps have to suffer.

And Gvim doesn't even try to recognize <C-/>. Sees it as single /.

mike3996
  • 16,360
  • 7
  • 64
  • 78
  • 5
    Thanks! I just mapped it to and it works! Original idea was to map to but that doesn't even register in vim, I think... – dev_nut Jan 29 '12 at 14:05
  • 2
    Is this only on iterm2? I'm using Terminal.app and just get the bell ding error and nothing happens. However, when I actually press the `_` key in the `` binding, it will do the comment toggle. How can I get Terminal.app to use the `` binding ? – skilbjo Aug 13 '16 at 09:28
  • 1
    Note on the above ^^ `` only works on the underscore on numeric keypads (10key), not on the apple compact keyboard. Additionally, `` is not a valid keycode (apparently there are limited `CTRL + letters` and `/` is not one of them). Source: http://vim.1045645.n5.nabble.com/How-to-map-Ctrl-td1192843.html – skilbjo Aug 17 '16 at 07:32
  • `` for terminal and iTerm works like a charm, but it doesn't for macvim. Did anyone happen to find a way around it by any chance? – AntK Oct 09 '17 at 01:44
23

Here is how you can do it regaining the selection if you are in visual mode:

nmap <C-_>   <Plug>NERDCommenterToggle
vmap <C-_>   <Plug>NERDCommenterToggle<CR>gv
Marcelo Lazaroni
  • 8,711
  • 3
  • 32
  • 40
4

Just to sum up the information from other answers. For me (there might be a difference due to the fact that I'm using neovim) <C-/> works fine on Windows, but I need to use <C-_> on Linux:

if has('win32')
  nmap <C-/> <leader>c<Space>
  vmap <C-/> <leader>c<Space>
else
  nmap <C-_> <leader>c<Space>
  vmap <C-_> <leader>c<Space>
endif
Hope
  • 951
  • 12
  • 17
  • I use neovide with `neovide --remote-tcp=localhost:6666` and running neovim in wsl, can not use nmap c – Good Pen May 10 '22 at 10:23
3

If you're using iTerm2 + vim, maybe the following steps can help you:

  1. Add following code to your .vimrc file.

    map ,cc <plug>NERDCommenterToggle

    or if you have defined your <leader> as ,

    map <leader>cc <plug>NERDCommenterToggle

  2. Check if you can use ,cc to toggle comments in vim

  3. Open iTerm2 -> Preferences -> Keys, click the + button

  4. Select Send Text with "vim" Special Chars, enter ,cc, like this.

  5. Now you can use C-/ to toggle comments in vim.

Terence
  • 111
  • 8