Skip to content

Instantly share code, notes, and snippets.

@introom
Created September 29, 2013 07:08
Show Gist options
  • Save introom/6750033 to your computer and use it in GitHub Desktop.
Save introom/6750033 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 line in getline(a:firstline, a:lastline)
if line =~? '^\s*' . cl
execute 's/^\(\s*\)' . escape(cl, '/') . '/\1/'
else
execute '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