我在 vim 中使用 tab 键时收到此错误。
Traceback (most recent call last):
File "< string >", line 1, in < module >
File "utils.py", line 15, in write
current_buffer.append('some text')
vim.error: Vim:E523: Not allowed here
我有当前的 .vim 文件
if expand("%:e") == 'xml'
inoremap <expr> <tab> WriteFile()
endif
function! WriteFile()
python3 utils.write()
endfunction
和这个 .py 文件
import vim
def write():
current_buffer = vim.current.buffer
current_buffer.append('some text')
最佳答案
出现此问题是因为您在计算映射表达式时不允许修改当前缓冲区。
参见 :help :map-expression
,更具体地说:
Be very careful about side effects! The expression is evaluated while obtaining characters, you may very well make the command dysfunctional. For this reason the following is blocked:
- Changing the buffer text (textlock).
- Editing another buffer.
- The
:normal
command.- Moving the cursor is allowed, but it is restored afterwards.
If you want the mapping to do any of these let the returned characters do that.
您应该使用函数返回要插入的字符,或者考虑使用非 <expr>
映射,显式为 :call
。
对于前者,返回要插入的字符:
inoremap <expr> <tab> WriteFile()
function! WriteFile()
return py3eval('utils.write()')
endfunction
和 utils.py
文件:
def write():
return 'some text'
或者,使用非 <expr>
映射的替代方法:
inoremap <tab> <C-O>:call WriteFile()<CR>
(后者可能有一些不需要的副作用,因为您将在修改缓冲区后返回到插入模式,但您将返回到映射之前的相同位置。您可能需要考虑使用 setpos()
或类似于在映射后移动光标,如果需要的话。)
关于python - Vim :E523: Not allowed here,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59440719/