【问题标题】:executing current python file in vim creates new file if current time is parsed如果解析当前时间,则在 vim 中执行当前 python 文件会创建新文件
【发布时间】:2013-05-07 09:13:42
【问题描述】:

我正在从 vim 执行 python 文件,如下所述: How to execute file I'm editing in Vi(m)

我在 Windows 和 Linux 上观察到相同的行为。 为了测试,我移动了我的 .vim 以避免其他插件干扰。然后我设置:

:set makeprg=python\ %

现在当我运行这样的示例文件(称为 mini.py)时

import datetime

print "hello"

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

foo1()

现在当我执行时

: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 会阻塞 datetime.now 输出,并使用当前日期创建一个新文件并立即显示它。

这种行为是意料之中的吗?

如果我注释掉 .now() 行(now().date() 显然不是问题),它会按预期工作,我或多或少会看到我期望的文本输出。

【问题讨论】:

    标签: datetime vim


    【解决方案1】:

    当你使用'makeprg' 时,Vim 会根据'errorformat' 解析输出以从输出中检索错误信息。您的日期输出看起来很像典型的错误消息,默认情况下,:make 会跳转到它遇到的第一个错误位置。

    你可以做什么:

    • 使用:make!(带砰);这将避免跳转到第一个错误。或者:
    • 除了设置'makeprg'之外,还要清除'errorformat'以避免Vim解析输出;除非你只用 Vim 编辑 Python 文件;您应该使用:setlocal,而不是全局:set,并将其放入~/.vim/after/ftplugin/python.vim
    :setlocal makeprg=python\ %
    :setlocal errorformat=
    

    【讨论】:

    • 所以基本上我会设置:au FileType python setlocal makeprg=python\ % au FileType python setlocal errorformat= 这是等价的,对吧?
    • 是的,这是等价的,但我的解决方案可以更好地适应许多此类设置(但需要:filetype plugin on)。
    猜你喜欢
    • 2019-05-17
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    相关资源
    最近更新 更多