Vim Tips Wiki
(Added automatic syntax check on save)
(Adjust previous/next navigation + minor manual clean)
Line 4: Line 4:
 
|previous=732
 
|previous=732
 
|next=734
 
|next=734
|created=May 26, 2004
+
|created=2004
 
|complexity=basic
 
|complexity=basic
 
|author=Rene Aguirre
 
|author=Rene Aguirre
Line 15: Line 15:
   
 
So, this is my suggestion, add these lines to your vimrc file:
 
So, this is my suggestion, add these lines to your vimrc file:
 
 
<pre>
 
<pre>
 
autocmd BufRead *.py set makeprg=python\ -c\ \"import\ py_compile,sys;\ sys.stderr=sys.stdout;\ py_compile.compile(r'%')\"
 
autocmd BufRead *.py set makeprg=python\ -c\ \"import\ py_compile,sys;\ sys.stderr=sys.stdout;\ py_compile.compile(r'%')\"
Line 25: Line 24:
   
 
As you are checking now, <F5> is mapped to execute the current script. Also I suggest you to use add the following lines to vimrc:
 
As you are checking now, <F5> is mapped to execute the current script. Also I suggest you to use add the following lines to vimrc:
 
 
<pre>
 
<pre>
 
autocmd BufRead *.py set tabstop=4
 
autocmd BufRead *.py set tabstop=4
Line 51: Line 49:
 
----
 
----
 
The counterpart of python's sys.stderr=sys.stdout in perl is the following line placed toward the beginning of the perl file:
 
The counterpart of python's sys.stderr=sys.stdout in perl is the following line placed toward the beginning of the perl file:
 
 
<pre>
 
<pre>
 
BEGIN {(*STDERR = *STDOUT) || die;}
 
BEGIN {(*STDERR = *STDOUT) || die;}
Line 59: Line 56:
   
 
----
 
----
 
Here's a script that automatically checks the syntax while saving and refuses to save files with syntax errors. Mildly hacky but it seems to work just fine.
 
<pre>
  +
" Define the current compiler
  +
if exists("compiler")
  +
finish
  +
endif
  +
let compiler = "python"
   
  +
" Set python as the make program and
Here's a script that automatically checks the syntax while saving and refuses to save files with syntax errors. Mildly hacky but it seems to work just fine :)
 
  +
setlocal makeprg=python
 
setlocal errorformat=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
   
 
" When writing Python file check the syntax
<pre>
 
 
au! BufWriteCmd *.py call CheckPythonSyntax()
" Define the current compiler
 
  +
if exists("compiler")
 
  +
function CheckPythonSyntax()
finish
 
 
" Write the current buffer to a temporary file, check the syntax and
endif
 
 
" if no syntax errors are found, write the file
let compiler = "python"
 
  +
let curfile = bufname("%")
 
  +
let tmpfile = tempname()
" Set python as the make program and
 
  +
silent execute "write! ".tmpfile
setlocal makeprg=python
 
 
let output = system("python -c \"__import__('py_compile').compile(r'".tmpfile."')\" 2>&1")
setlocal errorformat=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
 
  +
if output != ''
 
 
" Make sure the output specifies the correct filename
" When writing Python file check the syntax
 
 
let output = substitute(output, fnameescape(tmpfile), fnameescape(curfile), "g")
au! BufWriteCmd *.py call CheckPythonSyntax()
 
  +
echo output
 
  +
else
function CheckPythonSyntax()
 
  +
write
" Write the current buffer to a temporary file, check the syntax and
 
  +
endif
" if no syntax errors are found, write the file
 
 
" Delete the temporary file when done
let curfile = bufname("%")
 
  +
call delete(tmpfile)
let tmpfile = tempname()
 
  +
endfunction
silent execute "write! ".tmpfile
 
let output = system("python -c \"__import__('py_compile').compile(r'".tmpfile."')\" 2>&1")
 
if output != ''
 
" Make sure the output specifies the correct filename
 
let output = substitute(output, fnameescape(tmpfile), fnameescape(curfile), "g")
 
echo output
 
else
 
write
 
endif
 
 
" Delete the temporary file when done
 
call delete(tmpfile)
 
endfunction
 
 
</pre>
 
</pre>

Revision as of 02:27, 30 March 2010

Tip 733 Printable Monobook Previous Next

created 2004 · complexity basic · author Rene Aguirre · version 5.7


I just discovered Vim, I really liked 'split' capability, I'm so used to edit Python source code on SciTe editor, I really missed the default CTRL-1 to check the sintax and F5 to run the script...

So, this is my suggestion, add these lines to your vimrc file:

autocmd BufRead *.py set makeprg=python\ -c\ \"import\ py_compile,sys;\ sys.stderr=sys.stdout;\ py_compile.compile(r'%')\"
autocmd BufRead *.py set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
autocmd BufRead *.py nmap <F5> :!python %<CR>

Make your that python is in your path, now when you open any python file just type ":make" to get the syntax errors, use ":cnext", to move to next error, check the help on make (":help make") for more info on how to navigate errors.

As you are checking now, <F5> is mapped to execute the current script. Also I suggest you to use add the following lines to vimrc:

autocmd BufRead *.py set tabstop=4
autocmd BufRead *.py set nowrap
autocmd BufRead *.py set go+=b

That will make to use a 4 spaces for you tabstop (only visually), it avoids wrapping your code and will add a bottom scrollbar. Now I like vim a litle bit more.

Comments

How about having the line:

nmap <buffer> <F5> :w<Esc>mwG:r!python %<CR>`.

in the file $VIM/vimfiles/ftplugin/python.vim?

(I have the preceding line with 'python' replaced by 'perl' in the file $VIM/vimfiles/ftplugin/perl.vim )

(The adjective <buffer> in the map of <F5> restricts the map to the buffer. The map of <F5> as given in the tip is "permanent" in the sense that after opening a *.py file, the map remains for other non-*.py files.

Also, putting the map in a ftplugin-file means that it is available even when the python file-type is detected by means other than the .py extension -- means such as the command ':set ft=python' or the shebang.)


The counterpart of python's sys.stderr=sys.stdout in perl is the following line placed toward the beginning of the perl file:

BEGIN {(*STDERR = *STDOUT) || die;}

The preceding is needed so that the 'r' in the map of <F5> can capture stuff in stderr.


Here's a script that automatically checks the syntax while saving and refuses to save files with syntax errors. Mildly hacky but it seems to work just fine.

" Define the current compiler
if exists("compiler")
  finish
endif
let compiler = "python"

" Set python as the make program and
setlocal makeprg=python
setlocal errorformat=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m

" When writing Python file check the syntax
au! BufWriteCmd *.py call CheckPythonSyntax()

function CheckPythonSyntax()
  " Write the current buffer to a temporary file, check the syntax and
  " if no syntax errors are found, write the file
  let curfile = bufname("%")
  let tmpfile = tempname()
  silent execute "write! ".tmpfile
  let output = system("python -c \"__import__('py_compile').compile(r'".tmpfile."')\" 2>&1")
  if output != ''
    " Make sure the output specifies the correct filename
    let output = substitute(output, fnameescape(tmpfile), fnameescape(curfile), "g")
    echo output
  else
    write
  endif
  " Delete the temporary file when done
  call delete(tmpfile)
endfunction