flake8 / vim / python2 / python3

flake8 / vim / python2 / python3

  • Written by
    Walter Doekes
  • Published on

In 2015, I wrote a quick recipe to use Vim F7-key flake8 checking for both python2 and python3 using the nvie/vim-flake8 Vim plugin.

Here's a quick update that works today. Tested on Ubuntu/Zesty.

$ sudo apt-get install python3-flake8  # py3 version, no cli
$ sudo pip -H install python-flake8    # py2 version, with cli
$ sudo cp /usr/local/bin/flake8{,.2}   # copy to flake8.2
$ sudo sed -i -e 's/python$/python3/' /usr/local/bin/flake8  # update shebang
$ sudo mv /usr/local/bin/flake8{,.3}   # rename edited one to flake8.3

Get the two python_flake8.vim and flake8.vim files from the nvie repository as mentioned above. And patch them with these changes:

$ diff -pu /usr/share/vim/vim80/ftplugin/python_flake8.vim{.orig,}; \
    diff -pu /usr/share/vim/vim80/autoload/flake8.vim{.orig,}

--- /usr/share/vim/vim80/ftplugin/python_flake8.vim.orig  2017-12-03 10:44:17.343019719 +0100
+++ /usr/share/vim/vim80/ftplugin/python_flake8.vim 2017-12-03 10:45:11.305310916 +0100
@@ -46,7 +46,8 @@ endfunction
 " remapped it already (or a mapping exists already for <F7>)
 if !exists("no_plugin_maps") && !exists("no_flake8_maps")
     if !hasmapto('Flake8(') && !hasmapto('flake8#Flake8(')
-        noremap <buffer> <F7> :call flake8#Flake8()<CR>
+        noremap <buffer> <F7> :call flake8#Flake8("flake8.2")<CR>
+        noremap <buffer> <F8> :call flake8#Flake8("flake8.3")<CR>
     endif
 endif

--- /usr/share/vim/vim80/autoload/flake8.vim.orig 2017-12-03 10:52:39.487212782 +0100
+++ /usr/share/vim/vim80/autoload/flake8.vim  2017-12-03 10:51:30.107243223 +0100
@@ -10,8 +10,8 @@ set cpo&vim

 "" ** external ** {{{

-function! flake8#Flake8()
-    call s:Flake8()
+function! flake8#Flake8(flake8_cmd)
+    call s:Flake8(a:flake8_cmd)
     call s:Warnings()
 endfunction

@@ -66,11 +66,11 @@ function! s:DeclareOption(name, globalPr
     endif
 endfunction  " }}}

-function! s:Setup()  " {{{
+function! s:Setup(flake8_cmd)  " {{{
     "" read options

     " flake8 command
-    call s:DeclareOption('flake8_cmd', '', '"flake8"')
+    call s:DeclareOption('flake8_cmd', '', '"'.a:flake8_cmd.'"')
     " quickfix
     call s:DeclareOption('flake8_quickfix_location', '', '"belowright"')
     call s:DeclareOption('flake8_quickfix_height', '', 5)
@@ -105,9 +105,9 @@ endfunction  " }}}

 "" do flake8

-function! s:Flake8()  " {{{
+function! s:Flake8(flake8_cmd)  " {{{
     " read config
-    call s:Setup()
+    call s:Setup(a:flake8_cmd)

     if !executable(s:flake8_cmd)
         echoerr "File " . s:flake8_cmd . " not found. Please install it first."

Back to overview Newer post: Recap 2017 Older post: Availability during holiday December 2017