dev_nut
dev_nut

Reputation: 2542

How to map <C-/> to toggle comments in vim?

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?

Upvotes: 52

Views: 19376

Answers (5)

Anselmo Park
Anselmo Park

Reputation: 1011

Currently works well. I'm using vim 9.1 patch 1-650 in macOS Sonoma 14.5 M2 CPU.

Upvotes: 0

Marcelo Lazaroni
Marcelo Lazaroni

Reputation: 10247

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

Upvotes: 24

Terence
Terence

Reputation: 109

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.

Upvotes: 2

Hope
Hope

Reputation: 1181

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

Upvotes: 4

mike3996
mike3996

Reputation: 17537

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 /.

Upvotes: 74

Related Questions