VIM integration for yapf.
If you use Vundle, add to your .vimrc
:
Plugin 'pignacio/vim-yapf-format'
or with pathogen.vim:
cd ~/.vim/bundle
git clone git://github.com/pignacio/vim-yapf-format
NOTE:
If yapf
is not in your sys.path
you have to add the following line in
your .vimrc
:
let g:yapf_format_yapf_location = '/path/to/yapf'
Use the YapfFormat
command to format the current lines or range. Some
examples:
# Reformat current line
:YapfFormat
# Reformat current visual range
:'<,'>YapfFormat
# Reformat whole file
:%YapfFormat
# In general, reformat <range>
:<range>YapfFormat
The default style is pep8
. To change it, set the g:yapf_format_style
(for global style) or b:yapf_format_style
(for current buffer) to your
preffered style. Buffer variable takes precedende over the global one, to
allow single buffer overrides.
I use the following key bindings to reformat the whole file in normal mode, the current line in insert mode and the current range in visual mode:
map <C-o> :%YapfFormat<CR>
imap <C-o> <ESC>:YapfFormat<CR>i
vmap <C-o> :YapfFormat<CR>
Of course, the <C-o>
can be changed to any key you like ;)
This script is heavily inspired by clang-format.py