This is the link of the plug-in used this time. Please check the README etc. by yourself.
pip install autopep8
If you manage vim plugins with dein, copy and paste the following line into plugin_lazy.toml
. (If you are not using dein, use dein now)
[[plugins]]
repo = 'nvie/vim-flake8'
on_ft = ['python']
[[plugins]]
repo = 'tell-k/vim-autopep8'
on_ft = ['python']
It is possible to execute it automatically before pushing to git or automatically when saving the file, but it is scary even if it is executed too much, so it is automatic when you press sift + f on vim Set to perform formatting. Add the following to .vimrc.
"autopep8<sift>+Run with f
function! Preserve(command)
" Save the last search.
let search = @/
" Save the current cursor position.
let cursor_position = getpos('.')
" Save the current window position.
normal! H
let window_position = getpos('.')
call setpos('.', cursor_position)
" Execute the command.
execute a:command
" Restore the last search.
let @/ = search
" Restore the previous window position.
call setpos('.', window_position)
normal! zt
" Restore the previous cursor position.
call setpos('.', cursor_position)
endfunction
function! Autopep8()
call Preserve(':silent %!autopep8 --ignore=E501 -')
endfunction
autocmd FileType python nnoremap <S-f> :call Autopep8()<CR>
This is based on the Stackoverflow page at here.
--When you press F7 in vim's normal mode, Flake8 is executed and you can see the parts that are not pep8 compliant. --Similarly, if you press sift + f in normal mode, Autopep8 will be executed and the source code will be automatically formatted.
Recommended Posts