2020年3月1日日曜日

Automatically delete errored whitespace in .py with vim command

I'm using vim-flake8 for .py script and make it run flake8 to check codestyle when save. Some kind of error needs to be considered, but most spacing error is always necessary to be corrected. So I have wrote a script to delete whitespaces before flake8 in .vimrc.

Just making function and command. Auto run by autocmd when save.

" .py delete whitespace when save
function! DeleteWhitespace()
  try
    %s#\s\{1,}$##  " W293
    %s#\($\n\s*\)\+\%$##  " W291, W393
  catch
  endtry
endfunction
command! DeleteTrail call DeleteWhitespace()
autocmd BufWritePost *.py call DeleteWhitespace()
" .py auto vim-flake8 when save
autocmd BufWritePost *.py call Flake8()