5

When you execute the shell in vim via the :sh command, the shell that is set in the shell option is executed. However, is it possible to pass some information from the vim instance that you are running to the invoked shell?

Of course that would be easily doable if you could alter the shell option to have some environment variables prepended to it (e.g. a=foo b=bar /usr/bin/bash). However that doesn't seem to be possible. Is there some other way?

DJMcMayhem
  • 17,581
  • 5
  • 53
  • 85
hgiesel
  • 1,944
  • 2
  • 16
  • 25

2 Answers2

7

You can set or modify an environment variable in Vim like so:

:let $MY_ENV = 'coconuts!'

Child processes inherit the parent process's environment, so this is available when you start a new shell process with :sh (which is a child of the vim process:

:sh

$ echo $MY_ENV
coconuts!

It also works with NeoVim's :terminal:

:terminal

$ echo $MY_ENV
coconuts!

Also see :help :let-$.

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
2

Carpetsmoker's solution is probably more useful if you want to keep the same environment variable around long-term, but if you wanted to do this in a single line, you could use ! to execute the external shell, and pass it whatever you want. I'm a Linux hacker, so I can't say how well this will behave in Windows or OS X, but an example in my environment would be:

:! FOO=abc bash 

This would execute bash with $FOO set to 'abc'.

papidave
  • 121
  • 3