I want to source a file in the same shell process vim was started, from within vim as an ex command. As far as I know, vim creates a new subshell and run commands. In addition to that, If I source a file, and then start vim, vim does not detect the recently added environment variables. (VIm Actually do detect recent exported variables)

Basically, I want to conditionally add some directories to vim's path variable. For the condition, I need to check existence of an environment variable that is come from a file that is sourced.

So, I source a file. It adds some env variable to the current shell. Open vim, run a vim function that checks the existence of an environment variable; if it exists, then append some directories to vim's path variable.

One solution that came to my mind is that when sourcing that file, set an alias: vim='vim -c "set path+=/path/to/dirs/"'. But I want to implement the idea in vim script, if that is possible.

Os: linux vim: v8.2

  • 2
    Vim isn't a "subshell" or a shell process at all. The shell from which you run Vim spawns a child process...a Vim process. Then when you do something like :!ls Vim spawns its own child process in which a shell program runs. So what you're requesting isn't really applicable.
    – B Layer
    Sep 19, 2021 at 11:23
  • 2
    Not unilaterally. The target has to be involved...i.e. you'd have to set up some form of interprocess communication (e.g. shared pipe). Or if you're running tmux you could send commands to another shell's terminal.
    – B Layer
    Sep 19, 2021 at 11:45
  • 1
    @BLayer :) thanks for the hint. I will look into that.
    – Ali
    Sep 19, 2021 at 12:33
  • 1
    Sure. The key command is mkfifo. Super easy to use. Don't know if they'll fit the bill but it doesn't hurt to look.
    – B Layer
    Sep 19, 2021 at 12:34
  • 1
    That being said, if it's straightforward you might be better off just parsing the file you want to source (perhaps just grepping it) and setting up the environment directly in Vim (per my earlier comment). Okay, gotta go. Will check back later.
    – B Layer
    Sep 19, 2021 at 12:51

2 Answers 2


If you run under *nix (including MSYS2/Cygwin) then you can suspend Vim process with Ctrl-Z (see :h suspend). This will bring you back to the Vim's parent process (i.e. parent shell). Use fg to get back as usual. Won't work under plain Windows.


I guess I reached to what I needed but the answer to this question As @BLayer explained in comments, It can't be done uninternally.

you'd have to set up some form of interprocess communication (e.g. named pipe).

You can also read this post and the explanation after it for further information related to this question.

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.