Unfortunately, as far as I know Ctrl+/ cannot be bound to in vim. :(
But if you're not attached to that particular mapping the rest is pretty simple. Here's an example that uses Ctrl+M (simply because it's close to you're mapping):
noremap <C-m> :normal! I//<CR>
This works for C like files, but we can do better.
noremap <C-m> :<S-Left>exe "<S-Right>normal! I".b:commentType<CR>
autocmd BufReadPost *.[ch] let b:commentType='//' "C files
autocmd BufReadPost *.pl let b:commentType='#' "Perl files
This is a bit longer but it allows us to specify different types of comments for different types of files.
This works because when you visually select something and then press :, vim automatically fills in the a range of '<,'>
for you. This is the range of the selection. The <S-Left>
and <S-Right>
moves above are simply navigating around this pre-filled text.
EDIT
Making it a toggle button adds quite a bit of complexity. The big problem is determining whether or not the lines you have selected are comments or not. This becomes increasingly harder when we don't know what syntax starts a comment. I believe that the most robust way to do this will be to rely on vim's syntax highlighter to tell us when we are in a comment or not. Here's what I've got:
noremap <expr> <C-m> (synIDattr(synID(line("."), col("."), 0), "name") =~ 'comment\c') ?
\ ':<S-Left>exe "<S-Right>normal! ".b:unCommentCommand<CR>' :
\ ':<S-Left>exe "<S-Right>normal! ".b:commentCommand<CR>'
autocmd BufReadPost *.[ch] let b:commentCommand='I//' "Comment for C files
autocmd BufReadPost *.[ch] let b:unCommentCommand='^xx' "un-Comment for C files
autocmd BufReadPost *.pl let b:commentCommand='I#' "Comment for perl files
autocmd BufReadPost *.pl let b:unCommentCommand='^x' "un-Comment for perl
This will decide whether or not to comment or uncomment based on if your cursor is in a comment region or not. This isn't perfect because if your cursor is perhaps in the initial indentation of a comment it will not register as a comment. But this works reasonably well.
synIDattr(...)
finds the syntax group under the cursor
<expr>
allows us to do a conditional mapping
Of course for each file type you want to edit you will have to add another autocmd for the b:commentCommand
and b:unCommentCommand
. This takes the form of a normal command that will do the comment/un-comment for that file type.
Note: I relized that I could simplify all my <Left>
calls to a single <S-Left>
and have updated my answer above as well.
<c-/>
does not work reliably. See the faq:nmap <C-_> gcc
, but it works on Ctrl+/ press)