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?


2 Answers 2


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:


$ echo $MY_ENV

It also works with NeoVim's :terminal:


$ echo $MY_ENV

Also see :help :let-$.

  • coconuts, LOL. I just burst out laughing. Anyways, is it possible to pass any parameters to sh shell or even a command that we want to run? Commented Jul 25, 2016 at 19:51
  • @DurgaSwaroop Oops, missed your question. You can set the 'shell' setting for that: It is allowed to give an argument to the command, e.g. "csh -f". Commented Sep 19, 2016 at 2:10
  • @DurgaSwaroop also see: vi.stackexchange.com/q/7174/205
    – muru
    Commented Sep 19, 2016 at 12:52

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'.

Your Answer

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

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