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

Vim评论在行尾的延续

发布时间:2020-12-15 17:01:21 所属栏目:安全 来源:网络整理
导读:所以,假设我有一大堆代码 int i = 0; // Do some junk herecursor is hereif(i == 0){ blahblahblahblah;}blahblahblah; 是否有可能告诉vim当我按Enter键时,我想要它会导致以下结果: int i = 0; // Do some junk here // cursor is hereif(i == 0){ blahbla
所以,假设我有一大堆代码
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('//')

(编辑:李大同)

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

    推荐文章
      热点阅读