Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
" source: https://stackoverflow.com/a/24046914/2571881
let s:comment_map = {
\ "c": '\/\/',
\ "cpp": '\/\/',
\ "go": '\/\/',
\ "java": '\/\/',
\ "javascript": '\/\/',
\ "lua": '--',
\ "scala": '\/\/',
\ "php": '\/\/',
\ "python": '#',
\ "ruby": '#',
\ "rust": '\/\/',
\ "sh": '#',
\ "desktop": '#',
\ "fstab": '#',
\ "conf": '#',
\ "profile": '#',
\ "bashrc": '#',
\ "bash_profile": '#',
\ "mail": '>',
\ "eml": '>',
\ "bat": 'REM',
\ "ahk": ';',
\ "vim": '"',
\ "tex": '%',
\ }
function! ToggleComment()
if has_key(s:comment_map, &filetype)
let comment_leader = s:comment_map[&filetype]
if getline('.') =~ '\v^\s*' . comment_leader
" Uncomment the line
execute 'silent s/\v\s*\zs' . comment_leader . '\s*\ze//'
else
execute 'silent s/\v^(\s*)/\1' . comment_leader . ' /'
endif
else
echo "No comment leader found for filetype"
endif
endfunction
nnoremap <Leader>t :call ToggleComment()<CR>
vnoremap <Leader>t :call ToggleComment()<CR>
@dave-kennedy

This comment has been minimized.

Copy link

dave-kennedy commented Mar 30, 2018

I edited this to skip empty lines. Also removed the \v from the regex comparison as it is unnecessary, and added a missing comment.

See https://gist.github.com/dave-kennedy/2188b3dd839ac4f73fe298799bb15f3b.

Feel free to merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.