所以,假设我有一大堆代码
int i = 0; // Do some junk here<cursor is here>
if(i == 0){
blahblahblahblah;
}
blahblahblah;
Run Code Online (Sandbox Code Playgroud)
是否有可能告诉vim当我按Enter键时,我想要它会导致以下结果:
int i = 0; // Do some junk here
// <cursor is here>
if(i == 0){
blahblahblahblah;
}
blahblahblah;
Run Code Online (Sandbox Code Playgroud)
我知道它会为一个单独排队的评论做到这一点,但我无法弄清楚这一点.
我不\xe2\x80\x99t 知道是否有一个插件(但可能有一个),但是以下映射应该可以通过按 Enter 键添加一行(尽管有更多方法来添加行) ):
\n\n" Function that adds new line starting with comment symbol if line does not \n" start with comment, but contains it.\nfunction! s:NewLine(comsymb)\n let line=getline(\'.\')\n " Check whether we are in comment. Assumes syntax highlighting is working \n " correctly. Remove these lines if you never write \xe2\x80\x9c//\xe2\x80\x9d in a string literal\n if empty(filter(synstack(line(\'.\'), min([col(\'.\'), col(\'$\')-1])),\n \\ \'stridx(tolower(synIDattr(v:val, "name")), "comment")!=-1\'))\n return "\\n"\n endif\n let cidx=stridx(line, a:comsymb)\n if cidx==-1\n " No comments\n return "\\n"\n elseif cidx==0 || line[:(cidx-1)]!~#\'\\S\'\n " This assumes that vim own continuation works correctly: do not do work \n " that can be done by something else\n return "\\n"\n endif\n " Preserve tab indentation if any, correctly replace non-indent tabs with \n " spaces\n let nextline=substitute(line[:(cidx-1)], \'\\v^(\\s*)(\\S.*)$\',\n \\ \'\\=submatch(1).\'.\n \\ \'repeat(" ", strdisplaywidth(submatch(2), \'.\n \\ indent(\'.\').\'))\',\n \\ \'g\').a:comsymb\n " Preserve presence of a space after comment start mark\n if line[cidx+len(a:comsymb)] is# \' \'\n let nextline.=\' \'\n endif\n return "\\n".((col(\'.\')<col(\'$\'))?("\\e\\"_c0"):("\\<C-\\>\\<C-o>\\"_d0")).nextline\nendfunction\n\ninoremap <expr> <CR> <SID>NewLine(\'//\')\nRun Code Online (Sandbox Code Playgroud)\n