gpt4 book ai didi

vim - 在 .vimrc 中运行 shell 脚本(并处理输出)

转载 作者:行者123 更新时间:2023-12-04 17:05:30 25 4
gpt4 key购买 nike

我正在尝试从 .vimrc 文件中运行 shell 脚本(脚本中标记了三个问题):

function! CheckMe(file)
let shellcmd = 'checkme '.a:file

" Start the command and return 0 on success.
" XXX: How do you evaluate the return code?
execute '!'.shellcmd
if !result
return 0
endif

" Ending up here, the command returned an error.
" XXX: Where to you get the output?
let pair = split(output, '\S')
let line = pair[0]
let char = pair[1]

" Jump to the errenous column and line.
" XXX: Why does this not work?
normal '/\%'.line.'l\%'.char.'c'
return 1
endfunction

所以总结一下,你如何获得脚本的结果/输出,为什么跳转语句不起作用?

额外细节:
  • shell 脚本在成功时返回 0,在失败时返回 1。失败时,脚本将两个数字(行号和列号)打印到标准输出,用空格字符分隔。
  • 根据Vim docs ,“normal”关键字的参数是“像输入一样执行”,但显然情况并非如此。当我输入它时它工作正常(在正常命令模式下,没有前导':'),但不在脚本中(“E78:未知标记)。
  • 最佳答案

    function! CheckMe(file)
    let shellcmd = 'checkme '.a:file

    let output=system(shellcmd)
    if !v:shell_error
    return 0
    endif

    " Are you sure you want to split on non-blanks? This
    " will result in list of blank strings.
    " My variant:
    let [line, char]=split(output)

    " Normal is not an execute: this is what it will do:
    " «'/» means «Go to mark /», produces an error E78 because /
    " is not a valid symbol for mark. Than normal stops after error occured.
    " If you need to use variables in nomal use «execute 'normal '.ncmd».
    " And you can not use «normal» to perform search
    execute '/\%'.line.'l\%'.char.'c'
    " or
    call setpos('.', [0, line, char, 0])
    return 1
    endfunction

    According to the Vim docs, the argument of the "normal" keyword is "executed like it is typed", but apparently that is not the case. It works fine when I type it (in the normal command mode, without leading ':'), but doesn't in the script ("E78: Unknown mark).



    只需键入 «'/» 即可获得此错误。

    关于vim - 在 .vimrc 中运行 shell 脚本(并处理输出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2302846/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com