The vim-commentary plugin uses 'commentstring'
to define what a comment should look like. Some languages support multiple comment formats, e.g. //
& /* */
. Change 'commentstring
to match the format that you use
set commentstring=//%s
However, if a project uses two different formats then you can still it becomes more challenging. I would then suggest making mappings which set 'commentstring'
and then rest it after the commentary command is given with the CommentaryPost
autocmd:
function! s:AltCommentary()
let s:commentary_format = &commentstring
let &commentstring = '// %s'
augroup alt_commentary
autocmd!
autocmd User CommentaryPost let &commentstring = s:commentary_format |
\ autocmd! alt_commentary
augroup END
endfunction
xmap <leader>gc :<c-u>call <SID>AltCommentary()<cr>gv<Plug>Commentary
nmap <leader>gc :<c-u>call <SID>AltCommentary()<cr><Plug>Commentary
nmap <leader>gcc :<c-u>call <SID>AltCommentary()<cr><Plug>CommentaryLine