开发者

How to modify a Vim buffer with a python script?

The official Vim python interface documentation states, that modifying buffers with python is very simple, basically:

:py import vim
:py vim.current.buffer[0] = "Hello world"

However, python throws an exception, when I try to do that:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: bad argument type for built-in operation

Read-only access (e.g. :py print vim.current.buffer[0] works just fine. Am I missing something here?开发者_JS百科 Why can't I modify vim buffers with python scripts?

[Note: I'm using recent Vim 7.3]


Works for me, "Hello World" is inserted in buffer. Is your vim compiled with +python

I'm using version 7.3.162

EDIT

looking in the hg log for if_python.c I see a lot issues related to python, e.g. this one:

changeset:   2641:b803b2776880
tag:         v7-3-062
user:        Bram Moolenaar <bram@vim.org>
date:        Tue Nov 16 19:26:02 2010 +0100
files:       src/auto/configure src/configure.in src/if_python.c src/if_python3.c src/version.c
description:
updated for version 7.3.062
Problem:    Python doesn't work properly when installed in another directory
        than expected.
Solution:   Figure out home directory in configure and use Py_SetPythonHome()
        at runtime. (Roland Puntaier)

Which version are you on?


This may be an encoding issue. I've run into a very similar (but not identical) use case, e.g. inside a vim python function:

buf = vim.current.buffer
names = [x.name for x in triggers] #encoded as a default python unicode, e.g. u'foo'
names = [x.encode('utf-8') for x in names] # Force to utf-8
buf[:] = names #Now this works.

Without forcing it into utf-8, I get the same exception, which I think is related to how vim handles python's default (for me) us-ascii strings. After converting to utf-8 it works fine. I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜