Automatically format Python code in Vim

List of things to use

This is the link of the plug-in used this time. Please check the README etc. by yourself.

Install autopep8

pip install autopep8

Install vim plugin

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']

Editing .vimrc

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>

reference

This is based on the Stackoverflow page at here.

How to use

--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

Automatically format Python code in Vim
format in python
Image format in Python
Automatically format Python code into PEP8-compliant code with Emacs
Easily format JSON in Python
Generate QR code in Python
Format json with Vim (with python)
Character code learned in Python
I want to write in Python! (1) Code format check
I made an action to automatically format python code
[Python] Generate QR code in memory
Handle NetCDF format data in Python
Handle GDS II format in Python
Write selenium test code in python
Write a vim plugin in Python
Code tests around time in Python
Mode line when you open the appropriate Python code in Vim
Fourier series verification code written in Python
Automatically update Power BI Desktop in Python
Automatically generate Python Docstring Comment in Emacs
Write a simple Vim Plugin in Python 3
Get the EDINET code list in Python
Download files in any format using Python
Notes on using code formatter in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Python string format
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
python character code
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
[Python] Algorithm-aware code
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Let's create a customer database that automatically issues a QR code in Python
Python string format
Constant in python
nCr in Python.
Scons in Python3
Puyo Puyo in python