3

I was reading http://vimdoc.sourceforge.net/htmldoc/starting.html#initialization, but couldn't immediately find the answer. Is the $VIM environment variable hardcoded in its binary (not read from a config file)? The binary is built differently depending on the OS, so I'm guessing that's how the $VIM differs per OS.

2 Answers 2

2

The whole process can be read under :h $VIM:

To avoid the need for every user to set the $VIM environment variable, Vim will try to get the value for $VIM in this order:

  1. The value defined by the $VIM environment variable. You can use this to make Vim look in a specific directory for its support files. Example: setenv VIM /home/paul/vim
  2. The path from 'helpfile' is used, unless it contains some environment variable too (the default is "$VIMRUNTIME/doc/help.txt": chicken-egg problem). The file name ("help.txt" or any other) is removed. Then trailing directory names are removed, in this order: "doc", "runtime" and
    "vim{version}" (e.g., "vim54").
  3. For MSDOS, Win32 and OS/2 Vim tries to use the directory name of the executable. If it ends in "/src", this is removed. This is useful if you unpacked the .zip file in some directory, and adjusted the search path to find the vim executable. Trailing directory names are removed, in this order: "runtime" and "vim{version}" (e.g., "vim54").
  4. For Unix the compile-time defined installation directory is used (see the output of ":version").

Once Vim has done this once, it will set the $VIM environment variable. To change it later, use a ":let" command like this:

    :let $VIM = "/home/paul/vim/"

It is sometimes hard to find stuff in the vim help "immediately" but it is a great source. The vim helpfile is a mighty tool which will guide you a long way before you need this site, once you got the hang of it!

2
  • To be fair, this info is kinda buried. Though if you were able to find your way to starting.txt it's not too much of a stretch to search for $VIM. :helpgrep helps (no pun intended), but then you have to know how to display and navigate the quickfix window. Vim is so unfriendly to beginners, I don't fault anyone for questions like this. Jul 10, 2021 at 15:34
  • 1
    @BigMcLargeHuge I am sorry, I didn't intended to sound rude at all. I just wanted to hint the might of the vim help. Tried to adapt my answer, I hope it is clear now, that I was not at all angry about the question!
    – B.G.
    Jul 13, 2021 at 7:10
3

Linux view, for more details see Doktor OSwaldo answer.

The installation directory is defined during build (to be exact: It is defined when configuring the build.). The default value of $VIM is a subdirectory below the installation directory and is compiled in (<inst-dir>/share/vim on Linux).

You can override this by setting the environment variable VIM before starting Vim.

The compiled-in value of $VIM is part of the output from :version or vim --version. Just search for fall-back for $VIM.

Within Vim you can display the actual value by executing

:echo $VIM
3
  • What I found quite confusing for the beginner is that after downloading and installing vim, you would normally expect it to know where it was installed, or at least to show a very prominent message saying "warning - it will not work properly until you have set this environment variable"
    – Andy
    Dec 18, 2020 at 16:25
  • @Andy I don't understand. What won't work properly? Vim does know where it's installed and sets $VIM accordingly without any user intervention. This answer just describes how you might change it, though I'm not sure why you'd want to. Jul 10, 2021 at 15:36
  • @Big McLargeHuge That's not what I understand from my experience and from @Ralf's answer, at least for people who downloaded vim rather than compiling it themselves. I installed a binary of vim on my Ubuntu 20.02 (I'm not sure exactly how but I'm guessing probably with apt). It installed into /usr/share/vim/vim82 but the built-in VIM variable was set to /usr/share/vim so I had to override it in my .profile. I can't remember quite what it was that wasn't working, but I was trying to install plugins at the time and this definitely fixed the problem.
    – Andy
    Jul 12, 2021 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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