Skip to content

Instantly share code, notes, and snippets.

@Raimondi
Forked from introom/gist:6750033
Last active December 24, 2015 05:19
Show Gist options
  • Save Raimondi/6750062 to your computer and use it in GitHub Desktop.
Save Raimondi/6750062 to your computer and use it in GitHub Desktop.
" Toggle Comment
augroup toggle_comment
au FileType vim let b:comment_leader = '" '
au FileType c,cpp,java let b:comment_leader = '// '
au FileType sh,make,python let b:comment_leader = '# '
au FileType text let b:comment_leader = '% '
augroup END
function! ToggleComment() range
let cl = b:comment_leader;
for linenr in range(a:firstline, a:lastline)
let line = getline(linenr)
if line =~? '^\s*' . cl
execute linenr . 's/^\(\s*\)' . escape(cl, '/') . '/\1/'
else
execute linenr . 's/^\(\s*\)/\=submatch(1) . printf( "%s", cl)/'
endif
endfor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment