LoginSignup
13
15

More than 5 years have passed since last update.

pythonのコーディングを自動修正してくれるautopep8を導入する

Last updated at Posted at 2016-03-14

便利と評判のPyCharmを使ってみたものの、やはり使い慣れたvimに戻ってしまった今日この頃。
コーディング規約PEP8に準拠しているかをチェック、修正してくれるプラグインautopep8を入れることで、より快適にpythonを書ける環境を整えたい。

autopep8をインストールする

$ pip3 install autopep8

NeoBundleでvimプラグインを入れる

$ vim ~/.vimrc
NeoBundle 'tell-k/vim-autopep8'

設定追加
内容はvimでpythonのコーディングスタイルを自動でチェック&自動修正する - ton-tech-tonを参考に。

$ vim ~/.vimrc
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 -')
endfunction

autocmd FileType python nnoremap <S-f> :call Autopep8()<CR>

以上で、 Shift + F を押せば、pythonコードを修正してくれるようになる。

13
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
15