1

A plugin I use executes:

silent execute '%!cmd'

which fails because of a particular environment variable I have set that deliberately modifies the behaviour of cmd.

I can fix it with:

silent execute '%!ENV_VAR= cmd'

But I can't contribute that upstream because it won't work on Windows. I believe the Windows equivalent would be:

silent execute '%!set ENV_VAR= && cmd'

though I can't readily test it.

Is there a platform agnostic way of doing this, say invoking execute with a fresh environment?

  • 2
    Just let $ENV_VAR=newvalue | %!cmd | let $ENV_VAR=oldvalue? – Matt Sep 26 at 9:35
  • 1
    :facepalm: Thanks! (Will accept answer) – OJFord Sep 26 at 9:59
2

As any child process inherits the environment from its parent, you can temporarily redefine Vim's own environment before executing your tool:

let [temp, $ENV_VAR] = [$ENV_VAR, '']
%!cmd
let $ENV_VAR = temp
  • Setting a variable to an empty string is not exactly the same as unsetting it... Which is actually possible with unlet $ENV_VAR. A more complete solution would actually check that the environment variable is actually set before unsetting it. – filbranden Sep 26 at 14:48
  • @filbranden For environment variables that's precisely the same thing. – Matt Sep 26 at 15:03
  • 1
    @filbranden And so what? The link says that some applications can make a difference. But Vim does not. – Matt Sep 26 at 15:31

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.