vimscript 调用与执行 [英] vimscript call vs. execute

查看:33
本文介绍了vimscript 调用与执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在vimscript中,callexecute有什么区别?我应该在哪些场景/用例中使用一种和另一种?

In vimscript, what is the difference between call and execute? In what scenarios / use cases should I use one vs the other?

(免责声明,我知道 vim 中提供了广泛的在线帮助 - 我正在寻求对这个特定问题的简明答案).

(Disclaimer, I am aware of the extensive online help available within vim - I am seeking a concise answer to this specific question).

推荐答案

来自自己写插件和阅读别人代码的经验:

From the experience of writing my own plugins and reading the code of others:

:call 用于调用函数,例如:

:call is for calling functions, e.g.:

function! s:foo(id)
    execute 'buffer' a:id
endfunction

let target_id = 1
call foo(target_id)

:execute 用于两件事:

:execute is used for two things:

  1. 构造一个字符串并对其求值.这通常用于将参数传递给命令:

  1. Construct a string and evaluate it. This is often used to pass arguments to commands:

execute 'source' fnameescape('l:path')

  • 评估函数的返回值(可以说是相同的):

  • Evaluate the return value of a function (arguably the same):

    function! s:bar(id)
        return 'buffer ' . a:id
    endfunction
    
    let target_id = 1
    execute s:bar(target_id)
    

  • 这篇关于vimscript 调用与执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆