开发者

Programmatically execute vim commands?

开发者 https://www.devze.com 2023-01-03 13:05 出处:网络
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 t

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 开发者_C百科set expandtab to my .vimrc file, the test will pass.

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消