In :help vim9
I read
The Vim9 script syntax and semantics are used in:
- a function defined with the
:def
command- a script file where the first command is
vim9script
- an autocommand defined in the context of the above
- a command prefixed with the
vim9cmd
command modifier
Seems easy to read, but I can't get much stuff working.
Attempt 1
I've created a file.vim
with this content:
def! FunWithDef(n: number): number
return n * 2
enddef
and then run :so %
after w
riting it. Afterward, I can run :echo FunWithDef(3)
and get back 6
. So far so good.
Attempt 2
In :help vim9-reload
I read
When loading a Vim9 script a second time all existing script-local functions and variables are deleted, thus you start with a clean slate.
However, if I change the name of the function above and run :so %
, I can still run the function via the old name (and the new one as well, obviously). Why is that?
Attempt 3
If I add a line containing just vim9script
at the to of the script above, when I try to :so %
, I get E477: No ! allowed
. Why is that?
Attempt 4
If I remove the "erroneous" vim9script
, and define the following instead,
var FunWithLambda = (x) => x * 2
then, upon :so %
, I get E1124: ":var" cannot be used in legacy Vim script
, but I can't add a vim9script
line, as per the previous attempt.
Attempt 5
But I can try prepending vim9cmd
to the definition of the lambda,
vim9cmd var FunWithLambda = (x) => x * 2
which makes :so %
succeed, but trying to call :echo FunWithLambda(3)
fails with E117: Unknown function: FunWithLambda
(I can't tab-complete FunWithLambda
, indeed). Prepending :vim9cmd
to echo
doesn't change anything.