python - Vim :E523: Not allowed here

标签 python vim-plugin

我在 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/

相关文章:

vim - 在选择其中的项目后,如何才能使 QuickFix 窗口关闭?

python - 避免在终端中输入 "python"来打开 .py 脚本?

python - 使用Python按 block 解析文件并从每个 block 中提取信息

python - 通过node-red将Python连接到网页

vim - 如何在 Visual Studio Code vim 扩展中使用 vim(键绑定(bind))

vim - 为什么(Vim 插件)NetRW 浏览功能打不开目录和文件?

python - 在skimage中裁剪图像?

python - NLTK 在一个分类器中设置多个特征?

vim - 使用哪个jade.vim 插件?

vim - 使用调用跟踪调试 Vim 插件