VIM/Python不能向VIM返回值

2024-09-30 10:41:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为VIM创建一个Python插件来检测当前项目是否是Android项目。不幸的是,我无法让它将布尔值返回到VIM。从VIM中调用插件只会输出任何结果。下面的代码使用print命令,但我也尝试了vim.command("return {value}")并在脚本中设置vim变量。有什么见解吗?在

我有一个包含这些内容的插件文件

if !has('python')
    echo "Error: Required vim compiled with +python"
    finish
endif


" Get local path for the script, so we can import other files
let s:script_folder_path = escape( expand( '<sfile>:p:h' ), '\' )
let s:python_folder_path = s:script_folder_path . '/../python/'

" Start the python file in the scriptdir
function! s:startPyfile(fileName)
    execute "pyfile " . s:python_folder_path . a:fileName
endfunction
command! Detect call Detect()
function! Detect()
  call s:startPyfile("vim_detect.py")
endfunction

它叫维姆_检测.py里面有这个

^{pr2}$

Tags: thepath项目插件scriptfunctionvimfolder
1条回答
网友
1楼 · 发布于 2024-09-30 10:41:17

正如其他人评论的那样,您的示例代码(您应该将其浓缩为一个最小的示例)实际上并没有被调用。我从Python返回结果的方法是设置一个Vim变量,如下所示:

strValue = "just a test"
vim.command("let python_result = '%s'" % str(strValue).replace("'", "''"))

对于布尔值,只需将0/1作为一个数字返回,简化了逻辑:

^{pr2}$

相关问题 更多 >

    热门问题