Add vim mappings around yapf python formatter

This commit is contained in:
Kenneth Benzie 2016-04-03 13:49:50 +01:00
parent 6363c75535
commit 437476694c

View File

@ -1,2 +1,34 @@
" Python convention is to use 4 space tabs. " Python convention is to use 4 space tabs.
setlocal tabstop=4 shiftwidth=4 softtabstop=4 setlocal tabstop=4 shiftwidth=4 softtabstop=4
" Python formatting mappings {{{
" Supported yapf styles: pep8, google
let g:yapf_style='pep8'
" A wrapper function to restore the cursor position, window position,
" and last search after running a command.
function! Preserve(command)
" Save the last search
let last_search=@/
" Save the current cursor position
let save_cursor = getpos(".")
" Save the window position
normal H
let save_window = getpos(".")
call setpos('.', save_cursor)
" Do the business:
execute a:command
" Restore the last_search
let @/=last_search
" Restore the window position
call setpos('.', save_window)
normal zt
" Restore the cursor position
call setpos('.', save_cursor)
endfunction
nmap <leader>ff :call Preserve(
\ "silent exec '0,$!yapf --style ".g:yapf_style."'")<CR>
" TODO Add mapping for single line formatting
" TODO Add mapping for text block formatting
" }}}