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, 2019 at 9:35
  • 1
    :facepalm: Thanks! (Will accept answer)
    – OJFord
    Sep 26, 2019 at 9:59

1 Answer 1


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, '']
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, 2019 at 14:48
  • @filbranden For environment variables that's precisely the same thing.
    – Matt
    Sep 26, 2019 at 15:03
  • unix.stackexchange.com/q/27708/281844
    – filbranden
    Sep 26, 2019 at 15:06
  • 1
    @filbranden And so what? The link says that some applications can make a difference. But Vim does not.
    – Matt
    Sep 26, 2019 at 15:31

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.