In visual studio, that code :

for(int x=0;x<10;x++)

becomes that code :

for (int x = 0; x < 10; x++)


as soon as you hit enter. I was wondering, is it possible, and if so, how can I make so vim does the same ?

Thanks in advance !


I used the plugin https://github.com/rhysd/vim-clang-format.git and added those lines to my vimrc :

" Clang formater things :
let g:clang_format#style_options = {
            \ "BreakBeforeBraces" : "Allman",
            \ "AccessModifierOffset" : -4,
            \ "AllowShortIfStatementsOnASingleLine" : "true",
            \ "AlwaysBreakTemplateDeclarations" : "true",
            \ "Standard" : "C++11"}

" map to <Leader>cf in C++ code
autocmd FileType c,cpp,objc nnoremap <buffer><Leader>cf:<Cu>ClangFormat<CR>
autocmd FileType c,cpp,objc vnoremap <buffer><Leader>cf :ClangFormat<CR>
" if you install vim-operator-user
autocmd FileType c,cpp,objc map <buffer><Leader>x <Plug>(operator-clang-format)
" Toggle auto formatting:
nmap <Leader>C :ClangFormatAutoToggle<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.