Vundle是vim外掛程式管理工具,用來管理vim外掛程式,vim-autoformat是代碼格式化外掛程式
1.安裝Vundle(參考點擊開啟連結)
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
2.在.vimrc檔案中添加如下內容
set nocompatible " be iMproved, requiredfiletype off " required" set the runtime path to include Vundle and initializeset rtp+=~/.vim/bundle/Vundle.vimcall vundle#begin()" alternatively, pass a path where Vundle should install plugins"call vundle#begin('~/some/path/here')" let Vundle manage Vundle, requiredPlugin 'VundleVim/Vundle.vim'
Plugin 'Chiel92/vim-autoformat'
" All of your Plugins must be added before the following linecall vundle#end() " requiredfiletype plugin indent on " required" To ignore plugin indent changes, instead use:"filetype plugin on"" Brief help" :PluginList - lists configured plugins" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate" :PluginSearch foo - searches for foo; append `!` to refresh local cache" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal"" see :h vundle for more details or wiki for FAQ" Put your non-Plugin stuff after this line
3.重啟vim,在vim中執行:PluginInstall,安裝vim-autoformat
4.安裝 astyle
sudo apt-get install astyle
也可以安裝其他語言對應的格式化外掛程式,安裝完成後,會執行預設格式化設定
5.自訂astyle格式化,在.vimrc中添加如下代碼
let g:formatdef_my_cpp = '"astyle --style=google"'let g:formatters_cpp = ['my_cpp']au BufWrite * :Autoformat
代碼儲存時,會自動進行格式化