├── README.md └── plugin └── cfmt.vim /README.md: -------------------------------------------------------------------------------- 1 | # vim-cfmt 2 | 3 | A vim plugin using 4 | [gnu indent](https://www.gnu.org/software/indent/manual/indent.html) 5 | to auto format C source code. 6 | 7 | You will need to install `indent` on your system to use this plugin. 8 | If you are on a debian based system you can use apt to install it. 9 | ```bash 10 | apt-get install indent 11 | ``` 12 | 13 | Install using Pathogen or Vundle then set this in your `vimrc`: 14 | ```vimrc 15 | autocmd BufWritePre *.c,*.h Cfmt 16 | ``` 17 | 18 | You can configure the style of formating by using the `g:cfmt_style` 19 | variable. 20 | 21 | ```vimrc 22 | let g:cfmt_style = '-linux' 23 | let g:cfmt_style = '-kr' 24 | let g:cfmt_style = '-gnu' 25 | ``` 26 | 27 | ### License 28 | 29 | Distributed under the same terms as Vim itself. See `:help license`. 30 | -------------------------------------------------------------------------------- /plugin/cfmt.vim: -------------------------------------------------------------------------------- 1 | " C source code formating via GNU indent 2 | " 3 | " Documentation: 4 | " https://www.gnu.org/software/indent/manual/indent.html 5 | " 6 | " Install: 7 | " deb: apt-get install indent 8 | " rpm: yum install indent 9 | 10 | if exists("g:loaded_cfmt") 11 | finish 12 | endif 13 | let g:loaded_cfmt = 1 14 | 15 | if !exists("g:cfmt_style") 16 | let g:cfmt_style = '-gnu' 17 | endif 18 | 19 | command! Cfmt call s:IndentC() 20 | 21 | function! s:IndentC() 22 | let view = winsaveview() 23 | execute '%!indent ' . g:cfmt_style 24 | if v:shell_error 25 | %| " output errors returned by indent 26 | undo 27 | echohl Error | echomsg "indent returned error" | echohl None 28 | endif 29 | call winrestview(view) 30 | endfunction 31 | --------------------------------------------------------------------------------