r/neovim•
Redbeardt

How do you use vim.fn.* functions as methods with lua?

I was looking at vim.fn.getenv(), specifically at this part:

Can also be used as a method: GetVarname()->getenv()

And I got wondering whether there was syntax I'm unaware of that'd let me run getenv() as a lua method. I've tried various forms but I can't seem to get it to work any other way than with vim.fn.getenv('HOME').

Anyone know of syntax that'd enable you to apply these vimscript method recommendations in builtin.txt to lua? So that you could do something like 'HOME':getenv().

Posted Apr 23, 2022, 3:05 PM UTC

15
VerifiedSugarDaddy•

Vim already has vim.env method in lua since 5.0

For example:

:lua print(vim.env.HOME)
5
RedbeardtOPset noexpandtab•

Oh. I didn't know about that one. Neovim has so much in it. D:

2
cdb_11•

You maybe could implement it with lua metatables, but I just don't get why would anyone want to do such thing. Why can't you just use getenv 'HOME'?

3
RedbeardtOPset noexpandtab•

Well.. You can.. I guess. >_>

Maybe I'm taking this "do everything with lua" thing a bit too far.

3
wookayin•

Try :echo "HOME"->getenv() (vimscript, not lua).

GetVarname()->getenv() is a vimscript syntax (the documentation is about vimscript functions). But I feel such syntax is not quite often used in vimscripts other than 'chaining' usages (e.g., list->filter(...)->map(..)->join()). See also: :help method

1