2011-05-27 39 views
5

官方Vim的Python接口documentation狀態,與蟒蛇修改緩衝區是很簡單的,基本上是:如何用python腳本修改Vim緩衝區?

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

然而,蟒蛇拋出一個異常,當我嘗試這樣做:

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

閱讀 - 僅限訪問(例如:py print vim.current.buffer[0]工作正常,我在這裏丟失了什麼?爲什麼我不能用python腳本修改vim緩衝區?

[注意:我正在使用最近的Vim 7.3]

回答

3

適用於我,「Hello World」插入緩衝區。是你的vim與+python

我使用的版本7.3.162

編輯

看在汞日誌if_python.c我看到有關蟒蛇,例如很多問題編譯這一個:

changeset: 2641:b803b2776880 
tag:   v7-3-062 
user:  Bram Moolenaar <[email protected]> 
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) 

你在哪個版本?

+0

是的。只讀訪問(例如:':py print vim.current.buffer [0]''可以正常工作 – 2011-05-27 20:15:48

+0

我很困惑,可以肯定的是,當你嘗試執行'buffer [0] =「hello世界「? – 2011-05-27 20:19:37

+0

你已經說服我正確理解API,並且在我的Vim和​​/或python中有一些錯誤。爲了完整起見,我可以在最新的Vim 7.3.206中重現這個錯誤。其餘的屬於bug跟蹤器,而不是StackOverflow。 – 2011-05-28 10:52:44

0

這可能是一個編碼問題。我遇到了非常相似(但不完全相同)的用例,例如Vim的Python函數裏面:

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. 

沒有迫使它爲UTF-8,我得到了同樣的異常,我認爲這是關係到VIM如何處理Python的默認(對我來說)US-ASCII字符串。轉換爲utf-8後,它工作正常。我希望這有幫助。