tags:

views:

90

answers:

2

I'm interested in setting up a TDD environment for developing Vim scripts and rc files. As a simple example, say I want to have vim insert 8 spaces when I press the tab key. I would set up a script that did the following:

  • Launch vim using a sandboxed .vimrc file
  • press i
  • press tab
  • press esc
  • press :w test_out
  • assert that test_out contains ' '

by the default config in vim, this would fail. However, once I add set expandtab to my .vimrc file, the test will pass.

So the question is, how do I programmatically issue these commands? 'vim -c ' is close, but seems to only work for ex mode commands. Any suggestions? This question seem to be thoroughly google-proof.

+3  A: 

You can try vim -S <script in> and, for your convenience, the opposite vim -w <script out>.

For more details on these options check VIM's documentation for the -s and -w arguments.

Miguel Ventura
Thanks, this looks like what I need!
Ben Gartner
+1  A: 

Is using vim a requirement? If it isn't you are using the wrong tool for the job, sed might be more appropriate.

But if you must, you can do it with vim anyway, just call it ex

#!/bin/sh
# insert a tab as the first character of test_out
ex test_out <<EOF
s/^/^I/
wq
EOF

if [ `od -c test_out | awk 'NR == 1 {print $2}'` != '\t' ] ; then
    echo "assertion failed"; exit 1;
fi

Where ^I is actually a Tab. Although this is a very odd approach.

msw
Yes, the idea is to do TDD on vim plugins and the like. I suppose I could do TDD on an external shell script and then map to it from with in Vim, but that's not quite what I want. Largely I'm interested in better understanding what I am lifting from other people's .vimrc files and plugin directories.
Ben Gartner