]> code.delx.au - dotfiles/blob - .vim/commenter.vim
Enhanced commenting
[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 commentSymbol = ''
12
13 let commentMapping = {
14 \'###': ['python', 'sh', 'muttrc', 'sshconfig', 'make'],
15 \'///': ['ox', 'java', 'cpp', 'c', 'php'],
16 \'"""': ['vim'],
17 \'!!!': ['xdefaults']
18 \}
19
20 for commentChar in keys(commentMapping)
21 for name in commentMapping[commentChar]
22 if &filetype == name
23 let commentSymbol = commentChar
24 endif
25 endfor
26 endfor
27
28 if commentSymbol == ''
29 execute 'echo "ToggleCommentify has not (yet) been implemented for the file-type " . &filetype'
30 else
31 if isCommented == commentSymbol
32 call UnCommentify(commentSymbol) " if the line is already commented, uncomment
33 else
34 call Commentify(commentSymbol) " if the line is uncommented, comment
35 endif
36 endif
37 endif
38 endfunction
39
40 function! Commentify(commentSymbol)
41 execute ':s+^+'.a:commentSymbol.'+'
42 nohlsearch
43 endfunction
44
45 function! UnCommentify(commentSymbol)
46 execute ':s+'.a:commentSymbol.'++'
47 nohlsearch
48 endfunction
49