let s:comment_map = { \ 'c': '\/\/', \ 'cpp': '\/\/', \ 'java': '\/\/', \ 'vhdl': '--', \ 'python': '#', \ 'vim': '"', \ 'tex': '%', \ 'plaintex': '%', \ 'sh': '#' \ } let s:block_map = { \ 'c': ['\/\*', '\*\/'], \ 'java': ['\/\*', '\*\/'], \ 'cpp': ['\/\*', '\*\/'], \ 'python': ['"""', '"""'], \ 'vim': ['" {{{', '" }}}'] \ } function! MyCommenter(uncomment) range let comment = get(s:comment_map, &ft, '') if exists('g:custom_comment_map') let comment = get(g:custom_comment_map, &ft, comment) endif if !len(comment) return endif let search_range = a:firstline . ',' . a:lastline " Uncomment the line (when commenting this avoids multiple comment chars) execute search_range . 's/\(^\s*\)\(' . comment . '\s*\)\+/\1/e' if !a:uncomment " Comment execute search_range . 's/\(^\s*\)\(\S\)/\1' . comment . ' \2/e' endif nohl endfunction function! MyBlocker(unblock) range let block = get(s:block_map, &ft, []) if exists('g:custom_block_map') let block = get(g:custom_block_map, &ft, []) endif if !len(block) return endif if !a:unblock execute 'normal! ' . a:lastline . 'gg' execute 'normal! o' . substitute(block[1], '\', '', 'g') . "\" execute 'normal! ' . a:firstline . 'gg' execute 'normal! O' . substitute(block[0], '\', '', 'g') . "\" else execute 'normal! ?' . block[0] . "\" . 'dd' execute 'normal! /' . block[1] . "\" . 'dd' endif nohl endfunction command! -range CommenseComment silent ,call MyCommenter(0) command! -range CommenseUncomment silent ,call MyCommenter(1) command! -range CommenseBlock silent ,call MyBlocker(0) command! -range CommenseUnblock silent ,call MyBlocker(1)