.vvconfig/install/vim/vimconfig/ToggleComment.vim

27 lines
669 B
VimL
Executable File

function! ToggleComment()
let s:commentChar = '#'
if &filetype == 'cpp' || &filetype == 'h'
let s:commentChar = '//'
elseif &filetype == 'py'
let s:commentChar = '#'
elseif &filetype == 'vim'
let s:commentChar = '"'
endif
let currentLine = getline('.')
let variablePattern = '^\s*\V\k\+\>'
let s:isCommentLine = !(match(currentLine, variablePattern) != -1)
let s:isEmpty = (currentLine == '')
if s:isEmpty
echo "empty line"
return
elseif s:isCommentLine
echo "uncomment"
execute 's/^[ ]*' . escape(s:commentChar, '\/')
else
echo "comment"
execute 's/^/' . escape(s:commentChar, '\/')
endif
endfunction