0

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

Ali
  • 151
  • 6
  • 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 '21 at 11:23
  • @BLayer thanks for shedding light on the question. Feel free to edit the question. – Ali Sep 19 '21 at 11:31
  • @Quasímodo The problem is when I source a file and then start vim, vim does not detect the recently added environment variables. echo expand('$MYVAR')' does not print the value of the variable. – Ali Sep 19 '21 at 11:33
  • @Ali Yep. A child only inherits environment variables when it's spawned. All you can do is define the environment variable in Vim itself...:let $FOO = "value" – B Layer Sep 19 '21 at 11:37
  • @BLayer Is there a command that runs a command in another shell process (given a process ID and a command string)? I couldn't find something from googling. – Ali Sep 19 '21 at 11:44
  • 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 '21 at 11:45
  • Ugh, I meant "named pipe"...I was thinking of "shared memory" at the same time. :P – B Layer Sep 19 '21 at 12:30
  • 1
    @BLayer :) thanks for the hint. I will look into that. – Ali Sep 19 '21 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 '21 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 '21 at 12:51
  • @BLayer I want to automate thing and this way, I guess, it kinda defeats the purpose. – Ali Sep 19 '21 at 13:35
  • @BLayer I'm stupid. I mistyped the name of the env variable. Vim actually detects the exported env variables. I'll edit the question and add some explanation there. I am sorry. – Ali Sep 19 '21 at 13:46
  • 1
    Some information after the horizontal rule would be better in an answer than as part of the question. For that reason, please [edit] it out and place it in an answer. – D. Ben Knoble Sep 21 '21 at 21:25

2 Answers2

1

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.

Matt
  • 20,685
  • 1
  • 11
  • 24
1

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.

Ali
  • 151
  • 6