【问题标题】:vim: add comment macrosvim:添加注释宏
【发布时间】:2011-04-30 05:41:41
【问题描述】:

Vim 对我来说几乎是完美的。但是我还是想要一个行注释和块注释功能,我想知道如何在python和javascript中编写一个vimrc来做到这一点。

无插件

【问题讨论】:

  • 块注释功能是指选择一个可视模式的块,然后在其周围放置块cmets?还是什么?
  • 是的,选择一个可视模式的block,然后放置block cmets
  • 编辑了我的答案以包含一个函数,该函数将注释掉可视选择的块。
  • 虽然 OP 特别要求提供行或块 cmets,但我想我应该提一下这个 vimrc only 脚本,它将 javadoc 之类的 cmets 添加到函数中! stackoverflow.com/a/8026272/654789

标签: vim


【解决方案1】:
【解决方案2】:

tcomment 提供了一个与 vim 工作方式很好地集成的运算符: http://www.vim.org/scripts/script.php?script_id=1173

它支持的评论样式比已经提到的 nerdcomment 少。

【讨论】:

    【解决方案3】:

    如果你想要 c 风格的行 cmets(我相信这在 javascript 中是合法的),你可以在你的 .vimrc 中设置以下内容,这将注释掉光标(在正常模式下)当前所在的行。

    map \lo I/*<Esc>A*/<Esc>
    

    如果你想要python cmets,你可以这样做:

    map \lo I#<Esc>
    

    如果你只想有一个语句,你可以这样做:

    if match(expand("%:t"), ".py") != -1
      map \lo I#<Esc>
    else
      map \lo I/*<Esc>A*/<Esc>
    endif
    

    如果您正在编辑 .py 文件,它将使用 # 注释,否则使用 /* ... */ 注释。

    编辑:以下函数将通过检查文件类型,用适当的样式 cmets 注释掉一个可视选择的块。然后,您可以将其映射到函数后面的 vmap 语句之类的简单内容。

      function! BlockComment(top,bottom)
    
        " deal with filetypes that don't have block comments 
        let fileName = expand("%:t")
        echo fileName
    
        if fileName =~ "\.py" || fileName =~ "\.sh" || fileName =~ "\.pl"
            execute "normal I# "
            return
        elseif fileName =~ "\.vim"
            execute 'normal I" '
            return
        endif
    
        " for c-style block comments (should work for javascript)
        let topLine = line("'<")
    
        " the + 1 is because we're inserting a new line above the top line
        let bottomLine = line("'>") + 1
    
        " this gets called as a range, so if we've already done it once we need to
        " bail
        let checkLine = getline(topLine - 1)
        if (checkLine =~ '\/\*')
            return
        endif
    
        let topString = "normal " . topLine . "GO/*"
        let bottomString = "normal " . bottomLine . "Go*/"
    
        execute topString
        execute bottomString
    
      endfunction
    
      vmap <Leader>bco<CR> :call BlockComment()<CR>
    

    忽略古怪的语法高亮。看来语法高亮不支持 vimscript。

    【讨论】:

    • 你的意思是对光标行进行注释,但是如何选择一个可视块,并对其进行注释。
    • 我必须对此进行一些研究... :)
    • 我在 vimrc 中添加了 map,但是我该如何使用呢?
    • 使用可视模式选择文本块,然后使用bco调用该函数。 通常映射到\,而Enter。因此,当您选择了文本时,输入 \bco 然后按 Enter 就可以了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多