1

I am trying to retrieve the value of $MYVIMRC in my init.lua but I am coming up short because I don't understand what $MYVIMRC actually is.

It is not a global variable, is it?

None of these work:

vim.g.$MYVIMRC
vim.g.MYVIMRC
vim.g.myvimrc

What is $MYVIMRC and how can I access it from Lua?

1
3

$MYVIMRC is an environment variable that Vim adds to the current environment.

Environment variables are usually inherited by child processes from their parent process. For example, you can see $MYVIMRC in a shell that is a child process of Vim:

:!printenv | grep MYVIMRC

(I use this form rather than just :!echo "$MYVIMRC" to "prove" that the value is not inserted before the command is sent to the shell.)

I don't use lua in Vim but I know you can examine environment variables with os.getenv() so, for instance, this will print the expected value:

:lua print(os.getenv("MYVIMRC"))

In other words, you don't need any special Vim API to get the value.

2
  • Thank you for taking the time to provide both the explanation and the Lua code! The $ should have been a clue that it's an environment variable that is simply available in vim.
    – severin
    Jul 3 at 9:19
  • You're welcome. Maybe obvious in retrospect to you but others in the future might benefit from this Q&A. If not that aspect specifically then perhaps from the os.getenv() part. Overall it's not bad for your first question asked here, IMO. :) Cheers.
    – B Layer
    Jul 3 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.