免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5443 | 回复: 12
打印 上一主题 下一主题

VIM怎么实现删掉C的注释 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-14 12:09 |只看该作者 |倒序浏览
现在是加注释可以了
map ,cc :s#^#//<CR>:nohl<CR><C-o>

请问下有没办法用同样的组合键 加个判断(是不是以//开头),如果是就删掉//, 不是就加上?

同一个键不行..用别的也行啊... (不知道要怎么进行判断)

论坛徽章:
0
2 [报告]
发表于 2009-04-14 12:16 |只看该作者

回复 #1 fcymk2 的帖子

在命令模式下用:%s/^\/\/.*$//

不过这种问题发到shell版合适,关于c/c++去掉注释之类的问题在那里有很多人总结过
而且方法很多

论坛徽章:
0
3 [报告]
发表于 2009-04-14 12:18 |只看该作者

回复 #2 youshuang 的帖子

:g/\/\//d更简单

论坛徽章:
0
4 [报告]
发表于 2009-04-14 13:28 |只看该作者

回复 #2 youshuang 的帖子

谢谢了.现在可以删掉了.

那有没有办法把 删掉和注释做到一个快捷键里面呢?

用的是:s#^//##

:g/\/\//d  把整行给删了.



还有一个问下...快捷键 ctrl+/要怎么写啊?

我写<C-/>没反应....

[ 本帖最后由 fcymk2 于 2009-4-14 13:45 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2009-04-14 13:58 |只看该作者

回复 #4 fcymk2 的帖子

呵呵,你真有想法,在vi里还想着用快捷键
从来没用过的说

论坛徽章:
0
6 [报告]
发表于 2009-04-14 14:16 |只看该作者

回复 #5 youshuang 的帖子

可能不是算一个快捷键? 算键映射吧...

现在用  ,cc ,cu 注释 删除注释已经成功了..

感觉就是没有<C-/> <A-/>用的顺手呵,可能还没习惯吧

论坛徽章:
0
7 [报告]
发表于 2009-04-14 14:30 |只看该作者
用Alt+/来做map就可以
map <A-/> :s#^//##<CR>:nohl<CR><C-o>
用Ctrl+/就不行了-_-..好奇怪
map <C-/> :s#^//##<CR>:nohl<CR><C-o>

用:map看所有的键映射的话..这2个都可以看的到

[ 本帖最后由 fcymk2 于 2009-4-14 14:32 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2009-04-21 23:53 |只看该作者
这两天研究了下VIM的脚本...已经可以实现 用alt+/ 注释与反注释了..ctrl+/还是无法使用...原因不明.....
贴下脚本...目前使用还算正常..不知道还有没有BUG


noremap <A-/> :call MK2_Comment()<CR>
vnoremap <A-/> :<BS><BS><BS><BS><BS>call MK2_CommentV()<CR>
inoremap <A-/> <Esc>:call MK2_Comment()<CR>a

function MK2_Comment()
    let colNum=col(".")
    let lineNum=line(".")
    let isComment=1
    let isComment=s:IsComment(lineNum)
    call s:SetComment(isComment)
    call s:SetCursor(isComment, lineNum, colNum)
endfunction

function MK2_CommentV()
    let colNum=col(".")
    let lineNum=line(".")
    let lineStart=line("'<")
    let lineEnd=line("'>")
    let i=lineStart
    while i<=lineEnd
        let isComment=s:IsComment(i)
        if isComment==0
            break
        endif
        let i=i+1
    endwhile
    let i=lineStart
    while i<=lineEnd
        call cursor(i, 0)
        call s:SetComment(isComment)
        let i=i+1
    endwhile
    call s:SetCursor(isComment, lineNum, colNum)
endfunction

function s:IsComment(currentLineNum)
    let currentLine=getline(a:currentLineNum)
    let lineLen=len(currentLine)
    let i=0
    while i<lineLen
        if !((currentLine==' ') || (currentLine==' '))
            if (currentLine=='/' && currentLine[i+1]=='/')
                return 1
            else
                return 0
            endif
        endif
        let i=i+1
    endwhile
    "if match(line,"^\(\t\|\s\)*\/\/") != -1
    "   let isComment=1
    "endif   
    return 0
endfunction

function s:SetComment(isComment)
    if a:isComment==0   
        s/^/\/\//
    else
        s/^\(\(\t\|\s\)*\)\/\//\1/
    endif
endfunction

function s:SetCursor(isComment, lineNum, colNum)
    if a:isComment==0   
        call cursor(a:lineNum,a:colNum+2)
    else
        call cursor(a:lineNum,a:colNum-2)
    endif
endfunction

[ 本帖最后由 fcymk2 于 2009-4-22 00:48 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2009-04-22 00:09 |只看该作者

vi博大精深

vi真的是博大精深啊! :wink:

论坛徽章:
0
10 [报告]
发表于 2009-04-22 12:10 |只看该作者
用插件EnhancedCommentify
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP