51

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,476
  • 4
  • 29
  • 49

4 Answers4

73

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
  • 17,047
  • 9
  • 64
  • 80
  • 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? – Anton Kastritskiy Oct 09 '17 at 01:44
  • It's tmux. For whatever reason, "[doesn't exist](https://github.com/tmux/tmux/issues/697#issuecomment-268998386)" in tmux. Outside of a tmux session, it seems to work fine in modern terminals. – ABabin Mar 17 '23 at 16:35
24

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
  • 9,819
  • 3
  • 35
  • 41
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
  • 1,051
  • 13
  • 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
  • That is untrue. It might have been the case on old Windows systems but not anymore. At least on Windows 11, using Windows Terminal and neovim, `` isn't recognized and `` works as on Unix systems. – adamency Dec 09 '22 at 18:48
  • 1
    Secondly, this is a bad idea to remap another mapping. Always a better idea to go to the source, using the original command `nerdcommenter#Comment(0,"toggle")` (the replacement of the deprecated `NERDCommenterToggle`). This will prevent issues if some parts of the old mapping are remapped to something else in the future. – adamency Dec 09 '22 at 18:57
2

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
  • 109
  • 8