2013-03-20 57 views
0

我想配置我的.vimrc做一些自動語法檢查。 這是我的問題,我想自動改變某些語法的另一個。 我處理計算機程序中的特定字符= =; ,。 ({[<vim語法檢查空白

個例是勝於言:

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

如何通過外部C++壓頭過濾文件?雖然GNU indent說它不是爲C++設計的,但它工作得很好。如果沒有,你可以試試astyle。那麼你所要做的就是

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

這樣,即使使用其他編輯器的人也可以使用相同的縮進樣式。

+0

感謝您的分享,但我希望此格式不僅適用於C++。 如果可能的話,只需一個編輯器vim和一些插件,而不是更多的軟件。 – Max 2013-03-20 10:50:49