Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

:.!ls

and check that the output is

one
two
three

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
add comment

1 Answer

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'
this=that

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.