天天看點

vim的python代碼檢測工具

這裡介紹三個vim的插件, 一個是pyflakes,另外一個是pep8,還有它們的合體flake8

安裝

安裝很簡單,找到你的.vim目錄,把這兩個壓縮包解壓了放進去

去下面vim官方下下來.

pyflakes http://www.vim.org/scripts/script.php?script_id=2441

pep8 http://www.vim.org/scripts/script.php?script_id=2914

flake8 http://www.vim.org/scripts/script.php?script_id=4440

然後放到~/.vim/ftplugin/python

注意放的是使用者的.vim目錄.是以如果你用的是root賬戶,那就需要在/root/.vim放一份. 另外pep8需要安裝pep8 for python

#pip install -U pep8

配置

flakes 加到vimrc裡面

if has("gui_running") highlight SpellBad term=underline gui=undercurl guisp=Orange endif let g:pyflakes_use_quickfix = 1 "這是開關

pep8可以設定哪個鍵來檢測,預設F5

"let g:pep8_map='whatever key'

flake8

"Auto-check file for errors on write: let g:PyFlakeOnWrite = 1 "List of checkers used: let g:PyFlakeCheckers = 'pep8,mccabe,pyflakes' "Default maximum complexity for mccabe: let g:PyFlakeDefaultComplexity=10 "List of disabled pep8 warnings and errors: let g:PyFlakeDisabledMessages = 'E501' "Default height of quickfix window: let g:PyFlakeCWindow = 6 "Whether to place signs or not: let g:PyFlakeSigns = 1 "Maximum line length for PyFlakeAuto command let g:PyFlakeMaxLineLength = 100 "Visual-mode key command for PyFlakeAuto let g:PyFlakeRangeCommand = 'Q'

使用

flakes會直接在代碼編輯頁面提示, 有格式錯誤的會标紅

vim的python代碼檢測工具

而pep8則是在vim的輸出視窗提示

vim的python代碼檢測工具

而flake8直接可以在儲存的時候提示行和錯誤資訊

vim的python代碼檢測工具

對于flake8的快速修複 PyFlakeAuto功能

我們可以綁定它到Fx按鍵上

在vimrc裡面加上下面的map就可以直接按F6來修複pep8的錯誤

map <F6> :PyFlakeAuto<CR>

還有pychecker, pylint等檢測的,就不一一列出了.