2

当我运行时vi --version,我看到VIM - Vi IMproved 7.3了,但是当我运行以下脚本时,它打印出我有 7.2 版。为什么?

pathnamevi。_ which vi打印/usr/local/bin/vim,那--version就是7.3which gvimprints /usr/bin/gvim,并且也--version打印较新版本的 vim 。

echo $EDITOR打印vi

#!/usr/bin/python
import os
import sys
import os.path
import subprocess
import tempfile


def exec_vimcmd(commands, pathname='', error_stream=None):
"""Run a list of Vim 'commands' and return the commands output."""
try:
    perror = error_stream.write
except AttributeError:
    perror = sys.stderr.write

if not pathname:
    pathname = os.environ.get('EDITOR', 'gvim')

args = [pathname, '-u', 'NONE', '-esX', '-c', 'set cpo&vim']
fd, tmpname = tempfile.mkstemp(prefix='runvimcmd', suffix='.clewn')
commands.insert(0,  'redir! >%s' % tmpname)
commands.append('quit')
for cmd in commands:
    args.extend(['-c', cmd])

output = f = None
try:
    try:
        print "args are"
        print args
        subprocess.Popen(args).wait()
        f = os.fdopen(fd)
        output = f.read()
        print "output is"
        print output
        print "that's the end of the output"
    except (OSError, IOError), err:
        if isinstance(err, OSError) and err.errno == errno.ENOENT:
            perror("Failed to run '%s' as Vim.\n" % args[0])
            perror("Please set the EDITOR environment variable or run "
                            "'pyclewn --editor=/path/to/(g)vim'.\n\n")
        else:
            perror("Failed to run Vim as:\n'%s'\n\n" % str(args))
            perror("Error; %s\n", err)
        raise
finally:
    if f is not None:
        f.close()

exec_vimcmd(['version'])

打印的参数是

['vi', '-u', 'NONE', '-esX', '-c', 'set cpo&vim', '-c', 'redir! >/var/folders/86/062qtcyx2rxbnmn8mtpkyghs0r0r_z/T/runvimcmducLQCe.clewn', '-c', 'version', '-c', 'quit']
4

1 回答 1

1

找出分配给 的值pathname,并查看它是否同意which vimwhich gvim在命令提示符处输入。您的脚本正在查看您的$EDITOR环境变量,但是当您从命令行运行时(g)vim,它会搜索您$PATH以找到第一个命中。例如,您可能/opt/local/bin/vim从 CLI 运行,但从/usr/bin/vim您的脚本运行。

于 2013-03-12T15:19:36.297 回答