In command mode(Esc) enter :gg=G to use Vim's built-in indention engine.
| Command Part | Description |
|---|---|
| gg | start of file |
| = | indent (when equalprg is empty) |
| G | end of file |
You can set equalprg in your .vimrc to use a more sophisticated auto-formatting tool.
For example, to use clang-format for C/C++ put the following line in your .vimrc file:
autocmd FileType c,cpp setlocal equalprg=clang-format
For other file types, replace c,cpp with the filetype you want to format and clang-format with your preferred formatting tool for that filetype.
For example:
" Use xmllint for indenting XML files. Commented out.
"autocmd FileType xml setlocal equalprg=xmllint\ --format\ --recover\ -\ 2>/dev/null
" Tidy gives more formatting options than xmllint
autocmd FileType xml setlocal equalprg=tidy\ --indent-spaces\ 4\ --indent-attributes\ yes\ --sort-attributes\ alpha\ --drop-empty-paras\ no\ --vertical-space\ yes\ --wrap\ 80\ -i\ -xml\ 2>/dev/null