我想配置我的 .vimrc 来做一些自动语法检查。那是我的问题,我想通过另一个自动更改某些语法。我处理计算机编程中的特定字符,例如 = ; , . ( { [ <.
一个比文字更好的例子:
void bibi(int param1,char *words)
{
unsigned int locale=param;
cout<<words<<endl;
}
变成:
void bibi( int param1,char* words)
{
unsigned int locale = param;
cout << words << endl;
}
只需使用添加或删除一些空格进行格式化。
我写这个:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Formating of text in code
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! ChangeSpaces()
"" search and replace "= " or " =" or "= " to " = "
silent! %s/\s*[=]\s*/ = /g
endfunction
""autocmd CursorMovedI * call ChangeSpaces()
""autocmd BufWrite * call ChangeSpaces()
autocmd FileAppendPre * call ChangeSpaces()
但我没有结果,在这种情况下,如果我写 " i=e" ,他们什么也不做,但如果我写 'i= ',它的工作,正则表达式不运行,他们在结束后替换 "图案”。
顺便说一句,如果你有更“性感的方式”来做我想做的事,请告诉我。事实上,当我想添加一些其他特定的字符时,代码变成了:
"function! ChangeSpaces()
"" search and replace "= " or " =" or "= " to " = "
"silent! %s/\s*[=]\s*/ = /g
""" search and replace "( " or " (" or "(" to " ( "
"" silent! %s/\s*[(]\s*/ ( /g
""" search and replace "[ " or " [" or "[" to " [ "
"" silent! %s/\s*[[]\s*/ [ /g
""" search and replace ", " or " ," or "," to " , "
"" silent! %s/\s*[,]\s*/ , /g
""" search and replace "== " or " ==" or "==" to " == "
"" silent! %s/\s*[==]\s*/ = /g
""" search and replace "> " or " >" or ">" to " > "
"" silent! %s/\s*[>]\s*/ > /g
""" search and replace ">= " or " >=" or ">=" to " >= "
" silent! %s/\s*[>=]\s*/ >= /g
""" search and replace "< " or " <" or "<" to " < "
"" silent! %s/\s*[<]\s*/ < /g
""" search and replace "<= " or " <=" or "<=" to " <= "
"" silent! %s/\s*[=]\s*/ <= /g
"" let repl=substitute(cline,\s*[= ]\s*," = ", "g")
"" call setline(".",repl)
"" let cline=line(".")
"" let ccol=col(".")
"" call cursor(cline, ccol)
"endfunction
""autocmd CursorMovedI * call ChangeSpaces()
""autocmd BufWrite * call ChangeSpaces()
"autocmd FileAppendPre * call ChangeSpaces()
此致。
PS:我的错,我想要这种格式,对于我使用的每一种语言,而不仅仅是 C++。