3

I am executing python files from vim as described here: How to execute file I'm editing in Vi(m)

I observe the same behavior on Windows & Linux. For testing, I moved my .vim so as to avoid other plugins interfering. Then I set:

:set makeprg=python\ %

Now when I run an example file like this (called mini.py)

import datetime

print "hello"

def foo1():
    print "foo"
    print "str: " + str(datetime.datetime.now())
    print "str: " + str(datetime.datetime.now().date())

foo1()

Now when I execute

:make
"mini.py" 10L, 173C written
:!python mini.py  2>&1| tee /tmp/vew33jl/9
hello
foo
str: 2013-05-07 17:01:47.124149
str: 2013-05-07
"str: 2013-05-07 17" [New File]
(3 of 4): 47.124149

vim kind of chokes on the datetime.now output and creates a new file with the current date and instantly displays it.

Is this behavior to be expected?

If I comment out the .now() line (now().date() is not a problem apparently), it works as expected and I more or less see the text output that I'd expect.

Community
  • 1
  • 1
Matthias Kauer
  • 9,697
  • 5
  • 17
  • 19

1 Answers1

2

When you use 'makeprg', Vim parses the output according to 'errorformat' to retrieve error messages from the output. Your date output looks conspicuously like a typical error message, and by default, :make jumps to the first error location it encounters.

What you can do:

  • Use :make! (with bang); that will avoid the jump to the first error. Or:
  • In addition to setting 'makeprg', also clear the 'errorformat' to avoid that Vim parses the output; unless you're only ever edit Python files with Vim; you should use :setlocal, not the global :set, and put that into ~/.vim/after/ftplugin/python.vim:
:setlocal makeprg=python\ %
:setlocal errorformat=
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • so basically I would set: au FileType python setlocal makeprg=python\ % au FileType python setlocal errorformat= This is equivalent, right? – Matthias Kauer May 07 '13 at 11:49
  • Yes, that is equivalent, but my solution scales better for many such settings (but requires `:filetype plugin on`). – Ingo Karkat May 07 '13 at 12:33