Auto Python Indent Code In Vim With Yapf

Yapf is a great utility to autoindent Python Code. 

Let us install yapf with pip.

pip install yapf

Once you have yapf installed. 

Open your .vimrc file and add following mapping...

autocmd FileType python nnoremap <leader>y :0,$!yapf<Cr><C-o>

Exit out of vim and open your .py file using vim. Hit <leader>y, you would see your Python code indented nicely.