[vim/vim] $VIM and $VIMRUNTIME should not set for :terminal or :shell (#7696)

15 views
Skip to first unread message

mattn

unread,
Jan 17, 2021, 9:56:20 AM1/17/21
to vim/vim, Subscribed

Vim and NeoVim set VIM and VIMRUNTIME environment variables on startup. For example, NeoVim set VIMRUNTIME to /usr/share/nvim/runtime. If start :terminal on NeoVim and type vim, it cause errors since Vim use VIMRUNTIME that set by NeoVim.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

Bram Moolenaar

unread,
Jan 17, 2021, 10:17:31 AM1/17/21
to vim/vim, Subscribed


Yasuniro Matsumoto wrote:

> Vim and NeoVim set VIM and VIMRUNTIME environment variables on
> startup. For example, NeoVim set VIMRUNTIME to
> `/usr/share/nvim/runtime`. If start `:terminal` on NeoVim and type
> `vim`, it cause errors since Vim use VIMRUNTIME that set by NeoVim.

This will have to be fixed in Neovim. E.g. by using $NVIMRUNTIME.

Vim explictitly allows for specifying a different runtime directory, we
can't change that.


--
hundred-and-one symptoms of being an internet addict:
169. You hire a housekeeper for your home page.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

mattn

unread,
Jan 17, 2021, 10:29:07 AM1/17/21
to vim/vim, Subscribed

For example, Vim also can fix this easily.

diff --git a/src/os_unix.c b/src/os_unix.c
index 1edc7e66b..9e64e6a77 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -4231,6 +4231,8 @@ set_child_environment(
     setenv("COLUMNS", (char *)envbuf, 1);
     sprintf((char *)envbuf, "%d", t_colors);
     setenv("COLORS", (char *)envbuf, 1);
+    if (didset_vim) unsetenv("VIM");
+    if (didset_vimruntime) unsetenv("VIMRUNTIME");
 #  ifdef FEAT_TERMINAL
     if (is_terminal)
     {
@@ -4258,6 +4260,8 @@ set_child_environment(
     putenv(envbuf_Columns);
     vim_snprintf(envbuf_Colors, sizeof(envbuf_Colors), "COLORS=%ld", t_colors);
     putenv(envbuf_Colors);
+    if (didset_vim) putenv("VIM=");
+    if (didset_vimruntime) putenv("VIMRUNTIME=");
 #  ifdef FEAT_TERMINAL
     if (is_terminal)
     {

James McCoy

unread,
Jan 17, 2021, 10:46:27 AM1/17/21
to vim/vim, Subscribed

This will have to be fixed in Neovim. E.g. by using $NVIMRUNTIME.

This isn't just an Vim/Neovim issue. $VIM/$VIMRUNTIME are internal details to the running vim process, which shouldn't be exposed to child processes.

The same issue can occur if trying to run a different Vim version from within :terminal, since it will pick up $VIMRUNTIME from the parent Vim.

Dominique Pellé

unread,
Jan 17, 2021, 10:47:43 AM1/17/21
to vim/vim, Subscribed

@mattn wrote:

For example, Vim also can fix this easily.

  1. Your suggested fix still has side effects. If user has set VIMRUNTIME before running vim, then I would expect VIMRUNTIME to set in the terminal.

So it would be better to save the old values of VIMRUNTIME and VIM and restore them before starting a terminal.

  1. I also see that vim sets MYVIMRC.

mattn

unread,
Jan 17, 2021, 10:49:54 AM1/17/21
to vim/vim, Subscribed

Your suggested fix still has side effects. If user has set VIMRUNTIME before running vim, then I would expect VIMRUNTIME to set in the terminal.

Did you try this patch? This works without side effects.

Tony Mechelynck

unread,
Jan 17, 2021, 11:29:59 AM1/17/21
to vim/vim, Subscribed

Vim passes all enironment variables to child processes, including those which were set inside Vim after it started.

When I run a child process, e.g. by :! or :shell, I expect it to have access to $VIM and $VIMRUNTIME the way they were set in Vim, just like I expect it to have access to any environment variable set in my vimrc for use within Vim.

Dominique Pellé

unread,
Jan 17, 2021, 12:41:35 PM1/17/21
to vim/vim, Subscribed

@mattn wrote:

Did you try this patch? This works without side effects.

You're right, I now see it checks didset_vim & didset_vimruntime.

That said, if I do env | grep VIM I find 0 variable in my shell
and I type it in a vim terminal, I see:

  • VIM=/usr/local/share/vim
  • MYVIMRC=/home/pel/.vimrc
  • VIMRUNTIME=/usr/local/share/vim/vim82
  • VIM_SERVERNAME=
  • VIM_TERMINAL=802

Gary Johnson

unread,
Jan 17, 2021, 1:43:36 PM1/17/21
to vim...@googlegroups.com
On 2021-01-17, James McCoy wrote:

> This isn't just an Vim/Neovim issue. $VIM/$VIMRUNTIME are internal details to
> the running vim process, which shouldn't be exposed to child processes.

I disagree. It can be useful for shell configuration files to know
whether or not they are running as subshells of Vim, i.e., due to
a :sh command. Other programs set environment variables for their
subshells--tmux and git come to mind.

Please don't mess this up; leave at least $VIM if not $VIMRUNTIME
alone.

Regards,
Gary

Bram Moolenaar

unread,
Jan 17, 2021, 2:24:52 PM1/17/21
to vim/vim, Subscribed


> > This will have to be fixed in Neovim. E.g. by using $NVIMRUNTIME.
>
> This isn't just an Vim/Neovim issue. `$VIM`/`$VIMRUNTIME` are

> internal details to the running vim process, which shouldn't be
> exposed to child processes.

This is not about internal details, this is how Vim tells a child
process about the environment it was started in.


> The same issue can occur if trying to run a different Vim version from
> within `:terminal`, since it will pick up `$VIMRUNTIME` from the
> parent Vim.

Only when the shell does not set $VIMRUNTIME. If your shell startup
sets a different value that one will be used. If it does not, then the
Vim instance it was started from matters.


--
hundred-and-one symptoms of being an internet addict:
171. You invent another person and chat with yourself in empty chat rooms.


/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Shane-XB-Qian

unread,
Jan 18, 2021, 12:54:38 PM1/18/21
to vim/vim, Subscribed

why no one asked why you'll use neovim and vim both which embed one to another... :-)
same like your patch, looks it can un-set / re-set by bash cmd too, if really needed embed one to another...
or their env name perhaps should not same as bram said.. so far no use neovim yet.. 🤣

Reply all
Reply to author
Forward
0 new messages