Hacker News new | ask | show | jobs
by atechie 5119 days ago
>I'm a bit stumped on toggling comments, though. Any ideas?

    " comentify/uncommentify a line or a visual block"
     function! Komment()
     if getline(".") =~ '^[ \t]*#'
             let hls=@/
             s/^\([ \t]*\)#\(.*\)$/\1\2
             let @/=hls
         else
             let hls=@/
             s/^\([ \t]*\)\(.*\)$/\1#\2
             let @/=hls
         endif
     endfunction
     :vnoremap // :call Komment()<CR>
     :nnoremap // :call Komment()<CR>

    If you want to use c style comment : 
    function! Komment()
         if getline(".") =~ '^[ \t]*\/\*'
             let hls=@/
             s/^\(\s*\)\/\*\(.*\)\*\/\(\s*\)$/\1\2\3/
             let @/=hls
         else
             let hls=@/
             s/[ \t]*$//
             s/^\(\s*\)\(.*\)\(\s*\)$/\1\/\* \2 \*\//
             let @/=hls
         endif
     endfunction
     :vnoremap <silent> // :call Komment()<CR>
     :nnoremap  <silent> // :call Komment()<CR>
edited formatting