Petrie's answer is only partially true.
By partially I mean, take for example, bdelete
. Supposedly, as stated by vim's manual, it states that by providing a number N
identifying a buffer (as in using the number provided by ls
or ls!
), you can delete a buffer in vimscript. Well that is only true, if you provide a literal number, like bdelete 6
, you will delete buffer 6. However, if you pass a variable to bdelete
that contains the buffer number to delete like, bdelete g:buffer_to_delete
(where g:buffer_to_delete=6
), it will not work! In fact in vimscript you can not write :bdelete g:buffer_to_delete
or call bdelete g:buffer_t_delete
. However, you can treat the command as a string and execute it like so, exec "bdelete".g:buff_to_delete
, that will work!
Thus vimscript call
DOES NOT invoke user-defined or internal functions all the time. There are situations where exec
must be used because call
doesn't function as it is supposed to. This situation has been around for many years and still lingers today with vim 8.0. So be aware and follow the rule:
If you can't make the call
try exec
uting instead.
It is not clear when you can use call
and when you must use exec
for I have found the above situation occurring erratically. So start with call
then if it fails, exec
will work, as of yet I haven't found an instance when it doesn't, fortunately!
Note to Vimscript World: there are other nuances like the above that should be spelled out clearly in the vim documentation, or at least fixed in the vim code! I and many others I know have spent many many hours in years passed wasting time figuring out these nuances. I thought Emacs was truly terrible in this regard, but Vim too has its own undisclosed idiosyncrasies.
And before you say it....yes I have provided these issues years ago. I am only writing this now, because I feel a lot of sympathy for Yossi for going through this problem, because I had the same problem many years ago.
Yossi: keep an open mind with Vimscript it lacks quite a bit of common-sense, but if you keep your wits about yourself you can get much for your time spent on vim, than you would with say with Emacs! Trust me, I've been using Emacs and (G)Vi(m) for 30 years now. (G)Vi(m) is the way to go...Emacs is fast dying, and rightly so :-( {Hey Stallman, don't you think its time to admit Emacs sux? Why go to all the trouble and create Evil, when Vim is free?}
Keep pushing Yossi! It will pay off!
(Yes everybody, I know this reply is a year late, but I can bet you there are many other Yossi's out there)
Let's hope, Vim 9.0, will do a better job than Vim 8.0. (I hope I will be still alive to see 9.0!)
call
calls a function,execute
executes anex
command.call
vs.execute
, you can get more information from the manual::h call()
,:h :exe
.