gpt4 book ai didi

行尾的 Vim 注释延续

转载 作者:行者123 更新时间:2023-12-04 20:18:06 25 4
gpt4 key购买 nike

所以,假设我有一大段代码

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('//')

关于行尾的 Vim 注释延续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9732764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com