3

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?

5

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

  • 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? – Durga Swaroop Jul 25 '16 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". – Martin Tournoij Sep 19 '16 at 2:10
  • @DurgaSwaroop also see: vi.stackexchange.com/q/7174/205 – muru Sep 19 '16 at 12:52
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'.

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.