1

I want to compile and run a Python script using Gvim.

I have configured this in my _vimrc:

map <F5>:!D:\Python27\python.exe%

But when I complete the Python code and then enter :F5enter, I receive the error:

E448 Extra tail characters

How can I solve this?

Johnsyweb
  • 121,480
  • 23
  • 172
  • 229
Leon2248
  • 11
  • 2
  • 1
    You may find the [SingleCompile plug-in](http://www.topbug.net/SingleCompile/) useful, rather than write your own. – Johnsyweb Apr 13 '14 at 07:43
  • http://stackoverflow.com/questions/18948491/running-python-code-in-vim/18948530#18948530 – Kent Apr 14 '14 at 08:33

2 Answers2

0

Your first problem is that you do not normally compile python - you just run it.

Second I suspect that you need a space before the % in your configuration assuming that it gets replaced by the filename.

Steve Barnes
  • 24,968
  • 6
  • 54
  • 63
  • 1
    Well, when you run a python script it **is** *compiled* to bytecode first. In fact there exist a `compile` built-in that performs this step while `eval`/`exec` first compile the code and then execute it. – Bakuriu Apr 13 '14 at 10:50
0

You're missing spaces, in two important areas:

  • First, there must be whitespace separating the {lhs} (<F5> in your case) from the {rhs} (to what the mapping is expanded.
  • Second, the % that Vim will expand to the filename must be separated from the Python executable path, or else you'll end up with something like python.exeFoo.py, which the operating system doesn't understand.

Additionally:

  • You should select the proper modes for your mapping; these usually aren't invoked by :F5Enter (for which you would need :cmap, and omit the :), but rather by just pressing F5 from normal mode (:nmap).
  • You should use :noremap; it makes the mapping immune to remapping and recursion.
:nnoremap <F5> :!D:\Python27\python.exe %<CR>
Ingo Karkat
  • 154,018
  • 15
  • 205
  • 275