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