Vim Tips Wiki
(Set category Compiler + remove html entities)
(Fixed sentence to be readable.)
Tags: Visual edit apiedit
 
(7 intermediate revisions by 6 users not shown)
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 12: Line 12:
 
|category2=Python
 
|category2=Python
 
}}
 
}}
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...
+
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 syntax and F5 to run the script...
   
 
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 22: Line 21:
 
</pre>
 
</pre>
   
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 ("[http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:make}} :help make]") for more info on how to navigate errors.
+
Make sure 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 ("[http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode: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:
 
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
  +
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
  +
</pre>

Latest revision as of 11:25, 20 June 2017

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 syntax 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 sure 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