Wikia

Vim Tips Wiki

Changes: Combine quickfix steps

Edit

Back to page

(assign tip ID, add see also section, add category)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
Various packages are available to check source code for style compliance or to identify errors such as using a variable before a value is assigned to it. For Python code, two such packages are [http://pypi.python.org/pypi/pyflakes pyflakes] and [http://pypi.python.org/pypi/pep8 pep8]. It is convenient to configure Vim's <tt>:make</tt> command to invoke one of the programs and display any errors in the quickfix window. This tip shows how to run <tt>:make</tt> multiple times and combine the errors into one quickfix list.
+
Various packages are available to check source code for style compliance or to identify errors such as using a variable before a value is assigned to it. For Python code, two such packages are [http://pypi.python.org/pypi/pyflakes pyflakes] and [http://pypi.python.org/pypi/pep8 pep8]. It is convenient to configure Vim's <code>:make</code> command to invoke one of the programs and display any errors in the quickfix window. This tip shows how to run <code>:make</code> multiple times and combine the errors into one quickfix list.
   
After sourcing the following code, edit a Python file and type <tt>:Pycheck</tt> to check the code with both <tt>pyflakes</tt> and <tt>pep8</tt> (each of these needs to be installed).
+
After sourcing the following code, edit a Python file and type <code>:Pycheck</code> to check the code with both <code>pyflakes</code> and <code>pep8</code> (each of these needs to be installed).
 
<pre>
 
<pre>
 
" Do make with different makeprg settings.
 
" Do make with different makeprg settings.
Line 38: Line 38:
 
</pre>
 
</pre>
   
The script defines a user command (<tt>Pycheck</tt>) that invokes the <tt>DoMake</tt> function, specifying the names of the programs to be run to check the current file. Any changes to the file are saved. Each program name provided (there can be any number) is used to set the <tt>'makeprg'</tt> option, then <tt>:make!</tt> is run to invoke the program and set the quickfix list from the results. The <tt>!</tt> option avoids jumping to the first error, if any (later, the script jumps to the first error for the first program, if applicable), while <tt>silent</tt> prevents the display of normal messages that result from running <tt>:make</tt> in order to avoid the "Press Enter" prompt.
+
The script defines a user command (<code>Pycheck</code>) that invokes the <code>DoMake</code> function, specifying the names of the programs to be run to check the current file. Any changes to the file are saved. Each program name provided (there can be any number) is used to set the <code>'makeprg'</code> option, then <code>:make!</code> is run to invoke the program and set the quickfix list from the results. The <code>!</code> option avoids jumping to the first error, if any (later, the script jumps to the first error for the first program, if applicable), while <code>silent</code> prevents the display of normal messages that result from running <code>:make</code> in order to avoid the "Press Enter" prompt.
   
The quickfix list from each run of <tt>:make</tt> is appended to <tt>qflist</tt> (a variable initialised to <tt>[]</tt> or an empty list). When finished, <tt>qflist</tt> holds all quickfix messages resulting from running each of the programs. If the list is empty, the quickfix window is closed (if it was currently open from a previous run). Otherwise, the list is used to set Vim's quickfix list, and the quickfix window is opened and the first error displayed.
+
The quickfix list from each run of <code>:make</code> is appended to <code>qflist</code> (a variable initialised to <code>[]</code> or an empty list). When finished, <code>qflist</code> holds all quickfix messages resulting from running each of the programs. If the list is empty, the quickfix window is closed (if it was currently open from a previous run). Otherwise, the list is used to set Vim's quickfix list, and the quickfix window is opened and the first error displayed.
   
 
==See also==
 
==See also==

Latest revision as of 06:45, July 13, 2012

Tip 1667 Printable Monobook Previous Next

created November 19, 2010 · complexity basic · author Jjmurre · version 7.0


Various packages are available to check source code for style compliance or to identify errors such as using a variable before a value is assigned to it. For Python code, two such packages are pyflakes and pep8. It is convenient to configure Vim's :make command to invoke one of the programs and display any errors in the quickfix window. This tip shows how to run :make multiple times and combine the errors into one quickfix list.

After sourcing the following code, edit a Python file and type :Pycheck to check the code with both pyflakes and pep8 (each of these needs to be installed).

" Do make with different makeprg settings.
" Error lists from each makeprg are combined into one quickfix list.
command! Pycheck call DoMake('pyflakes', 'pep8')
function! DoMake(...)
  update  " save any changes because makeprg checks the file on disk
  let savemp = &makeprg
  let qflist = []
  for prg in a:000
    let &makeprg = prg . ' %'
    silent make!
    let qflist += getqflist()
  endfor
  if empty(qflist)
    cclose
  else
    call setqflist(qflist)
    copen
    cfirst
  endif
  let &makeprg = savemp
endfunction

The script defines a user command (Pycheck) that invokes the DoMake function, specifying the names of the programs to be run to check the current file. Any changes to the file are saved. Each program name provided (there can be any number) is used to set the 'makeprg' option, then :make! is run to invoke the program and set the quickfix list from the results. The ! option avoids jumping to the first error, if any (later, the script jumps to the first error for the first program, if applicable), while silent prevents the display of normal messages that result from running :make in order to avoid the "Press Enter" prompt.

The quickfix list from each run of :make is appended to qflist (a variable initialised to [] or an empty list). When finished, qflist holds all quickfix messages resulting from running each of the programs. If the list is empty, the quickfix window is closed (if it was currently open from a previous run). Otherwise, the list is used to set Vim's quickfix list, and the quickfix window is opened and the first error displayed.

See alsoEdit

CommentsEdit

Around Wikia's network

Random Wiki