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

Vim评论在行尾的延续

所以,假设我有一大堆代码
int i = 0; // Do some junk here<cursor is here>

if(i == 0){
  blahblahblahblah;
}

blahblahblah;

是否有可能告诉vim当我按Enter键时,我想要它会导致以下结果:

int i = 0; // Do some junk here
           // <cursor is here>

if(i == 0){
  blahblahblahblah;
}

blahblahblah;

我知道它会为一个单独排队的评论做到这一点,但我无法弄清楚这一点.

我不知道是否有一个插件(但可能有一个),但是下面的映射应该可以通过按Enter来添加一行(有很多方法可以添加该行):
" Function that adds new line starting with comment symbol if line does not 
" start with comment,but contains it.
function! s:NewLine(comsymb)
    let line=getline('.')
    " Check whether we are in comment. Assumes Syntax highlighting is working 
    " correctly. Remove these lines if you never write “//” in a string literal
    if empty(filter(synstack(line('.'),min([col('.'),col('$')-1])),\   'stridx(tolower(synIDattr(v:val,"name")),"comment")!=-1'))
        return "\n"
    endif
    let cidx=stridx(line,a:comsymb)
    if cidx==-1
        " No comments
        return "\n"
    elseif cidx==0 || line[:(cidx-1)]!~#'\S'
        " This assumes that vim own continuation works correctly: do not do work 
        " that can be done by something else
        return "\n"
    endif
    " Preserve tab indentation if any,correctly replace non-indent tabs with 
    " spaces
    let nextline=substitute(line[:(cidx-1)],'\v^(\s*)(\S.*)$',\           '\=submatch(1).'.
                \             'repeat(" ",strdisplaywidth(submatch(2),'.
                \                                          indent('.').'))',\           'g').a:comsymb
    " Preserve presence of a space after comment start mark
    if line[cidx+len(a:comsymb)] is# ' '
        let nextline.=' '
    endif
    return "\n".((col('.')<col('$'))?("\e\"_c0"):("\<C-\>\<C-o>\"_d0")).nextline
endfunction

inoremap <expr> <CR> <SID>NewLine('//')

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

相关推荐