Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm hacking on a test framework for vimscript (an exercise in futility, I know; humor me). Some vim plugins make calls out to the shell (with the system() function or the ! command). I want the test driver to be able to grab those calls and return pre-baked data.

As far as I can tell, vim's system() does a fork and runs the command in a subshell. In theory, it should be possible to do something like

alias ls="echo one\ntwo\nthree"

so that we can have a test along the lines of:


and check that the output is


In practice, vim seems to be forking out a subshell that doesn't respect any temporary aliases. In other words, it makes a new clean shell environment using the user's shell (zsh, bash, whatever) but not the user's shell settings (global nor local).

Do you know of any way to run vim in an environment such that certain system calls are controlled by the user?

share|improve this question
up vote 3 down vote accepted

Non-interactive shells don't go through all the initialization that an interactive shell does. See the INVOCATION section of the bash manual page (or whatever shell you're targeting) for details.

On the other hand, bash will look for a script named in $BASH_ENV and execute it before starting a non-interactive shell. Use that to set up your environment for vim's shell escapes. Commandline demonstration:

% bash -c 'alias'
<no output>
% export BASH_ENV=./special.aliases 
% bash -c 'alias'

But you can actually do much better than that: set the SHELL variable to a program of your choice, perhaps just /bin/echo, and vim will call it instead of launching a hard-to-control shell. This works even if your command includes a pipeline or other shell metacharacters, so I highly recommend it. (You'll get the -c that vim inserts when forking the "shell", but I'm sure you can deal with that).

share|improve this answer
Awesome. One question: Is there a zsh equivalent? It's hard to know what shell vim will be running in, and I know I can't support all of them, but hitting all the big players would be nice. – So8res Feb 1 '13 at 17:48
You'll have to look that up in the zsh manual yourself. Or use the second method (redefine $SHELL), as I recommended. The shell that vim is running under is irrelevant: Vim will launch whatever $SHELL tells it to. – alexis Feb 1 '13 at 18:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .