Luc Hermitte (talk | contribs) m (Added to Python category) |
m (Reverted edits by 86.10.6.81 (talk | block) to last version by Fritzophrenic) |
||
(8 intermediate revisions by 6 users not shown) | |||
Line 4: | Line 4: | ||
|previous=732 |
|previous=732 |
||
|next=734 |
|next=734 |
||
− | |created= |
+ | |created=2004 |
|complexity=basic |
|complexity=basic |
||
|author=Rene Aguirre |
|author=Rene Aguirre |
||
|version=5.7 |
|version=5.7 |
||
|rating=41/16 |
|rating=41/16 |
||
+ | |category1=Compiler |
||
+ | |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 |
+ | 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'%')\" |
||
autocmd BufRead *.py set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m |
autocmd BufRead *.py set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m |
||
− | autocmd BufRead *.py nmap |
+ | autocmd BufRead *.py nmap <F5> :!python %<CR> |
</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 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. |
||
− | As you are checking now, |
+ | 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 36: | Line 36: | ||
<pre> |
<pre> |
||
− | nmap |
+ | nmap <buffer> <F5> :w<Esc>mwG:r!python %<CR>`. |
</pre> |
</pre> |
||
Line 43: | Line 43: | ||
(I have the preceding line with 'python' replaced by 'perl' in the file $VIM/vimfiles/ftplugin/perl.vim ) |
(I have the preceding line with 'python' replaced by 'perl' in the file $VIM/vimfiles/ftplugin/perl.vim ) |
||
− | (The adjective |
+ | (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.) |
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.) |
||
Line 49: | 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;} |
||
</pre> |
</pre> |
||
− | The preceding is needed so that the 'r' in the map of |
+ | 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. |
||
− | [[Category:Python]] |
||
+ | <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> |
Revision as of 02:15, 15 March 2014
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 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