vim语法检查空格

时间:2013-03-20 09:03:59

标签: regex vim whitespace syntax-checking

我想配置我的.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 ++。

1 个答案:

答案 0 :(得分:1)

如何通过外部C ++压头过滤文件?虽然GNU缩进表示它不是为C ++设计的,但它的工作原理相当不错。如果没有,您可以尝试astyle。那么你所要做的就是

map <F8> :w<CR>m':%!astyle<CR>`'

即使使用其他编辑器的人也可以使用相同的缩进样式。