微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vim:添加评论宏

Vim对我来说几乎是完美的.但我仍然想要一个行注释和块注释功能,我想知道如何编写一个 vimrc来在 pythonjavascript中执行此操作.

没有插件

解决方法

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

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

如果您需要python注释,可以执行以下操作:

map \lo I#<Esc>

如果您只想要一个语句,您可以这样做:

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

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

编辑:以下函数将通过检查文件类型注释具有适当样式注释的可视选择块.然后,您可以将其映射到类似函数后面的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感知.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐