Vim快速注释/解除注释一行C代码

加个快捷键以及简单function到.vimrc中,如下:

用法:按Alt+/注释一行,再按一次解除注释。

 1 " Comment a line hotkey
 2 map <A-/> :call CommentLine()<CR>
 3 
 4 """""""""""""""""""""""""""""""""""""""""""""""""""""""
 5 """"""""""""""""""" Functions """""""""""""""""""""""""
 6 """""""""""""""""""""""""""""""""""""""""""""""""""""""
 7 function CommentLine()
 8     let isCommented = 0
 9     let curline = line(".")
10     let linestr = getline(curline)
11     let linelen = len(linestr)
12     let headend = 0
13 
14  " Check : weather the line has been commented. Get the headend
15  "   while headend < (linelen - 1)
16  "       if !(linestr[headend] == ' ' || linestr[headend] == ' ')
17  "           if linestr[headend] == '/' && linestr[headend + 1] == '*'
18  "               let isCommented = 1
19  "               break
20  "           endif
21  "       else
22  "           let headend = headend + 1
23  "       endif
24  "   endwhile
25 
26     if linelen > 2 && linestr[0] == '/' && linestr[1] == '*'
27         exec "normal! 0xx$xx/"
28     else
29         exec "normal! 0i"
30     endif
31 
32 endfunc

posted on 2012-03-12 11:44  William.Wu  阅读(1673)  评论(0编辑  收藏  举报

导航