In vimscript, what is the difference between call
and execute
? In what scenarios / use cases should I use one vs the other?
(Disclaimer, I am aware of the extensive online help available within vim - I am seeking a concise answer to this specific question).
Short answer
You may see
call
as first evaluate the expression and then discard the result. So only the side effects are useful.Long answer
Define:
Call:
Output:
Execute:
Output:
Execute:
Output:
See Switch to last-active tab in VIM
for example
:exe "tabn ".g:lasttab
Where g:lasttab is a global variable to store the current tab number and that number is concatenated with "tabnext" to switch e.g to tab number 3 (If g:lasttab e.g. contains '3' for example)
That whole string >"tabn ".g:lasttab< is evaluated and executed by VIM's exec command.
HTH?
From the experience of writing my own plugins and reading the code of others:
:call: Used to call functions:
:execute: Used for two things:
1) Construct a string and evaluate it. This is often used to pass arguments to commands:
2) Evaluate the return value of a function (arguably the same):
:call
: Call a function.:exec
: Executes a string as an Ex command. It has the similar meaning ofeval
(injavascript
,python
, etc)For example: