]> code.delx.au - dotfiles/blob - .vim/commenter.vim
Vim config improvements
[dotfiles] / .vim / commenter.vim
1 " Commenting of lines! Stolen & modified from vim.org's ToggleCommentify
2 map <C-c> :call ToggleCommentify()<CR>j
3 imap <C-c> <ESC>:call ToggleCommentify()<CR>j
4 " The nice thing about these mapping is that you don't have to select a visual block to comment
5 " ... just keep the CTRL-key pressed down and tap on 'c' as often as you need.
6
7 function! ToggleCommentify()
8 let lineString = getline(".")
9 if lineString != $ " don't comment empty lines
10 let isCommented = strpart(lineString,0,3) " getting the first 3 symbols
11 let fileType = &ft " finding out the file-type, and specifying the comment symbol
12 if fileType == 'ox' || fileType == 'java' || fileType == 'cpp' || fileType == 'c' || fileType == 'php'
13 let commentSymbol = '///'
14 elseif fileType == 'vim'
15 let commentSymbol = '"""'
16 elseif fileType == 'python' || fileType == 'sh'
17 let commentSymbol = '###'
18 else
19 execute 'echo "ToggleCommentify has not (yet) been implemented for this file-type"'
20 let commentSymbol = ''
21 endif
22 if isCommented == commentSymbol
23 call UnCommentify(commentSymbol) " if the line is already commented, uncomment
24 else
25 call Commentify(commentSymbol) " if the line is uncommented, comment
26 endif
27 endif
28 endfunction
29
30 function! Commentify(commentSymbol)
31 execute ':s+^+'.a:commentSymbol.'+'
32 nohlsearch
33 endfunction
34
35 function! UnCommentify(commentSymbol)
36 execute ':s+'.a:commentSymbol.'++'
37 nohlsearch
38 endfunction
39