加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

vim:添加评论宏

发布时间:2020-12-15 21:20:53 所属栏目:安全 来源:网络整理
导读:Vim对我来说几乎是完美的.但我仍然想要一个行注释和块注释功能,我想知道如何编写一个 vimrc来在 python和 javascript中执行此操作. 没有插件 解决方法 如果你想要c风格的行注释(我认为在javascript中是合法的),你可以在.vimrc中设置以下内容,它将注释掉光标(
Vim对我来说几乎是完美的.但我仍然想要一个行注释和块注释功能,我想知道如何编写一个 vimrc来在 python和 javascript中执行此操作.

没有插件

解决方法

如果你想要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感知.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读